Despite the significant experimental effort made in the last decades, the origin of the ultra-high energy cosmic rays is still largely unknown. Key astrophysical information to identify where these energetic particles come from is provided by their chemical composition. It is well known that a very sensitive tracer of the primary particle type is the muon content of the showers generated by the interaction of the cosmic rays with air molecules. We introduce a likelihood function to reconstruct particle densities using segmented detectors with time resolution. As an example of this general method, we fit the muon distribution at ground level using an array of counters like AMIGA, one of the Pierre Auger Observatory detectors. For this particular case we compare the reconstruction performance against a previous method. With the new technique, more events can be reconstructed than before. In addition the statistical uncertainty of the measured number of muons is reduced, allowing for a better discrimination of the cosmic ray primary mass.