I would probably proceed like this:
Create a hash. The numbers will be the keys, the numbers of occurrences will be the values. Once the input is finished, I would create another hash, the number of occurences being the keys, and (references to) the lists of numbers being the values.