For example: I have an array here: {0,1,2,3,4,0,4} then the output should be {0,4}.
Can anyone help me? .)
For example: I have an array here: {0,1,2,3,4,0,4} then the output should be {0,4}.
Can anyone help me? .)
anyone?
Help…
So if the first array contained integers from 0-100 rather than 0-4?
Assuming your array contains integers, but you can really do this on every datastructure that can be linear sorted.
Now iterate over T and use a modified max search algorithm to get the key with the highest count.
Example:
{0,3,6,23,4,4,8,6,4,7,6,6}
Sorted: {0,3,4,4,4,6,6,6,7,8,23}
=> T = { {0,1}, {3,1}, {4,3} ,{6,3},{7,1},{8,1},{23,1} }
Modified Max Search:
Create a dynamic array A.
Use a standard max search algorithm, but push the max element into A. Everytime you see a value that is equal to your current max, push it also into A. If you come across a value that is greater than A, remove all elements from A and push the new max elem into A. Repeat.
After that just return A.
Time complexity:
Sorting is dominant here: O(n log n)
Going throught the array and grouping: O(n)
Going through the grouped array (worst case all elements have the same amount of occurance): O(n)
Deleting all elements in Array A: O(n)
So overall you have O(n log n + 3n) = O(n log n). you can reduce the overhead of 3n if you don’t group etc. to n.
Whatever. Here you go.
In the worst case you have a runtime complexity of O(n^2) with that is if NewArray contains the exact same elements as the original array (no duplicates at all.) Also not speaking of the overhead introduced by AddUnique. It’s probably faster for smaller amounts, but for higher 1000+ this will introduce significant perforamcen problems I guess.
Yep,it seems like a kind of way to solve. But I just took an example. What if the length of array is 100 or more?
I didn’t test it, but there this should work.