It depends on whether you need the most efficient or easiest to write method.
1) if you know that all numbers will be from 0 to 1000, you just create an array of 1000 zeros (arrays), go through your array and increase the correct entry position. Then you sort these occurrences and select the N-dimensional value.
2) You have a βbagβ of unique items, you look at your numbers, check if this number is in the bag, if not, you add it, if it is here, you just increase the number of entries. Then you select the smallest number from it.
A bag can be a linear array, a BST, or a dictionary (hash table).
The question is " Nth most common", so I think you cannot avoid sorting (or a clever data structure), so better complexity cannot be better than O (n * log (n)).
source share