When astronomers started to get spectra of stars and began classifying them, the initial classification was based on the strength of the Balmer absorption lines in the spectra. The Balmer lines are created by electons in hydrogen atoms that are currently in the second energy level (N=2) absorbing energy and jumping up to higher levels. The stars with the strongest lines were given a designation of A, the second strongest B, etc. on down the line.
Later, the association with temperature was made and many of the classes were combined and reordered based on the temperature of the star. However, the main letter designations were kept even though they were now "out of order". So the letters actually correspond to the relative strength of the Hydrogen absorption lines.
The reason the O stars, which are hottest, are so far down on the scale is that they are so hot that Hydrogen is fully (or very nearly fully) ionized. If the atom has been ionized, it by definition doesn't have any electrons in the N=2 level since it doesn't have any electrons at all. Since there are no electrons in the proper level to form the line, the line is weak as it is only created when a chance recombination occurs.
The cooler M stars, at the other end of the main sequence, also show very week Hydrogen lines. The reason for this is that they are too cool to even get the electrons excited enough to get up to the N=2 energy state. If there are no electrons in N=2, there is no way to get the electron to absorb energy and jump higher and form the Balmer lines. What hydrogen lines are seen here are due to the few higher velocity atoms on the long tail of the velocity distribution. When they collide, they have enough energy to get their electrons up to N=2.
As the temperature increases, the energy available to excite the atoms increases as well. As you move up from M class stars you get to spectral types K, G, F, and then A, each one increasing in strength of the lines. The A stars, at about 10,000 K, are just the right temperature to push almost all the hydrogen atoms into the higher N=2 state where they can then be knocked higher and produce the Balmer series. Since there are a lot of electrons in N=2, you get strong lines. The next spectral class up is B, which has weaker lines because at this point you've started to ionize the hydrogen atoms and therefore have fewer atoms with electrons in the N=2 state. And then comes O stars which are nearly fully ionized and so are very week in the Balmer series.
As has been said, this is probably a very subjective question/answer. Not only that, but the composition of galaxies, and even regions within a galaxy, varies a great deal. Then there is the question of what constitutes as being part of the galaxy as opposed to perhaps a small orbiting dwarf galaxy. The answer you got from the Quora seems to be pretty comprehensive.
The volume of an area of interest, divided by the number of stars in that area seems to be the one that most people take as the approach. Which may not get a very accurate result, but smoothed out over said volume. Although, I will note that the first technique given on the quora site gives an answer that is close to the accepted "average" in the Milky Way, so at least there doesn't seem to be a large disagreement there. Of course, that assumes that the same initial starting conditions are used in both problems, which is highly unlikely since they aren't totally agreed upon anyway.
EDIT TO ADD: For more examples of similar math, here Dr. Plait calculates the number of habitable planets (where he shows the calculation for the volume of the galaxy). Making some assumptions of our own (like 200,000,000,000 stars which is LOW in my opinion), we come out to an average distance of about 5 light years. Doubling the number of stars gives an average of about 4 light years though, so again, we are not off by factors.
Best Answer
You can use an HR diagram along with calibrated evolutionary models to find the distance (and in some cases, mass and age) of individual stars. The method is known as spectroscopic parallax. This is a confusing name because it is not a parallax measurement at all.
The technique is to use spectroscopy, or less precisely the colours, of a star to estimate it's effective temperature and surface gravity. This can normally be used to get a good idea of what kind of star (dwarf, giant and spectral type). This is enough to locate it in the HR diagram and determine the absolute luminosity. From there, the apparent brightness of the star yields its distance.
The technique works best for main sequence stars. Other types have quite age-dependent positions in the HR diagram and since this isn't known and also because the gravity is not usually accurate enough to pin down the exact HR diagram position (although it is usually good enough to distinguish a main sequence star from a giant or subgiant), it doesn't work as well.
Another simple description of this technique, with examples, is given here.