how to find the count of an element which repeats maximum number of times in an array if the array contains both positive and negative elements in most efficient way ? asked 27 Jun '14, 14:53

The question has been closed for the following reason "The question is answered, right answer was accepted" by garakchy 29 Jun '14, 00:56
I am not sure can you try this with map? Take a look at this codehere By using MAP we have get the count with lesser time complexity i mean by O(n)=log n. I guess that will be better than O(n) :) If you can tell the question then we can give a try. This is the code i submitted to find out the maximum count element in an array. I mean for the MAX_COUNT problem. answered 27 Jun '14, 15:19
Can you please tell whether it is working or not? Or tell the question?
(27 Jun '14, 18:39)
2
this is the best method
(28 Jun '14, 14:16)
1
@kuruma: thanks dude. It is a bit disappointing that even after debugging the whole wrong code submitted here and theses ppl they dont even turn back after getting the benifit :( Not even just a thanks or an upvote. I dont know whether he is satisfied with this method :\ Anyway thanks @kuruma :D glad to find you here again :)
(28 Jun '14, 17:00)
how is this the best method? the element access time of a map is O(log(n)). the element access time(element look up) of an array is always O(1). So, when you update the frequency at some index in a map, it takes O(log(n)) but it takes only O(1) in an array. So, if both methods can be used and the time limit is strict, shouldn't we try to use an array This is something to consider when we solve the problem.
(28 Jun '14, 17:20)
Read the question properly @pratku123. the range is between 10^9 to 10^9. How can you store the frequency of negative numbers in array index?
(28 Jun '14, 19:51)
@bipin2 Thanks for the clarification. In that case, this method is the best. Good Answer!
(28 Jun '14, 22:24)
@pratku123: thanks bro!! Suggestions are always welcome :)
(28 Jun '14, 22:38)
showing 5 of 7
show all

Both the methods mentioned in answers before my answer, answered 27 Jun '14, 19:49

Well I have an idea. If the numbers can be negative or large (eg: 10^9), then arrays cannot be used. Then we are using map and time complexity will be nlogn. But this will also have lot of dynamic memory allocation factors, etc that slow down the code. So, a simpler way is to sort the array in O(nlogn) and then traverse it to find the most repeating element. answered 28 Jun '14, 17:40

@suryanit: If you have understood the answer can you please accept the answer so that we can close this question . :)