Software engineer internship interview questions shared by candidates
Given an array of n elements and a number which occurs more than n/2 times in the array, find the number. O(nlogn) then O(n).
Use a counter and a result variable. Initialize it to 0. Traverse through the array. If the current number is same as result increase counter else decrease counter. Once counter becomes 0 for a particular value of result, change the result to the new value. The value of result at the end of the array traversal is your answer.