With regards to Big O notation what is the best method of solving the following problem?
Given a String array of size N. Iterate through the array and return a single string containing the string that was repeated in the array the most. Also the string returned should repeat the string the number of times it was in the array. EX. if in the array "Bob" was the string found most and it was found 3 times the string returned should be "BobBobBob"
In thinking of how to do this I was thinking of using a HashMap or something else that would force unique key values, store the string as the key and the frequency as the value. When its all said and done that would leave me to iterate through the map comparing values..
Overall this would give me O(n) + O(n) ... im just wondering if there is a better way to do it
EDIT:
I understand that you cant do better than O(n) but the issue here is that once i iterate over the initial array to build the HashMap I will then have to reIterate over the hashmap to figure out which pair has the highest value which will again be O(n) .. essentially is there a way to do this where i only have to iterate the entire list One time
this would give me O(n) + O(n)do you meanO(n)time andO(n)space complexity?O(n)time when the worst case size of the output isO(n)bytes.O(n)...arraytag to this. Then every word in the title would be a tag :)