Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Frequency of numbers in a given group of numbers

Suppose we have a vector/array in C++ and we wish to count which of these N elements has maximum repetitive occurrences and output the highest count. Which algorithm is best suited for this job.

example:

int a = { 2, 456, 34, 3456, 2, 435, 2, 456, 2}

the output is 4 because 2 occurs 4 times. That is the maximum number of times 2 occurs.

like image 932
Abhishek Mishra Avatar asked Sep 28 '08 09:09

Abhishek Mishra


3 Answers

Sort the array and then do a quick pass to count each number. The algorithm has O(N*logN) complexity.

Alternatively, create a hash table, using the number as the key. Store in the hashtable a counter for each element you've keyed. You'll be able to count all elements in one pass; however, the complexity of the algorithm now depends on the complexity of your hasing function.

like image 132
Franci Penov Avatar answered Oct 31 '22 14:10

Franci Penov


Optimized for space:

Quicksort (for example) then iterate over the items, keeping track of largest count only. At best O(N log N).

Optimized for speed:

Iterate over all elements, keeping track of the separate counts. This algorithm will always be O(n).

like image 37
Sklivvz Avatar answered Oct 31 '22 16:10

Sklivvz


If you have the RAM and your values are not too large, use counting sort.

like image 43
Thorsten79 Avatar answered Oct 31 '22 15:10

Thorsten79