The time complexity of general algorithms is given below:
Algorithm | Best | Worst | Average --------------+-----------+-----------+---------- MergeSort | O(n lg n) | O(n lg n) | O(n lg n) InsertionSort | O(n) | O(n^2) | O(n^2) QuickSort | O(n lg n) | O(n^2) | O(n lg n) HeapSort | O(n lg n) | O(n lg n) | O(n lg n) BinarySearch | O(1) | O(lg n) | O(lg n)
In general, when you go through the list to fulfill certain criteria, you really cannot do anything better than linear time. If you need to sort an array, I would say stick with Mergesort (very reliable) to find the element with the highest frequency in the array.
Note It is assumed that you want to use a sorting algorithm. Otherwise, if you are allowed to use any data structure, I would go with a structure like hashmap / hashtable with a constant search time. Thus, you simply map the keys and update a pair of key frequency values. Hope this helps.
David source share