Houzz Interview Question

Write a program that identifies which element has been repeating (n/2 + 1) times?

Interview Answer

Anonymous

Aug 16, 2016

- Write a for loop to go through the elements in an array - In the for loop use an object to store elements as keys and its occurrence as value - If the element does not exist create a new element in the object with value 1 - If the element exists then increase its occurrence by 1 each time you find it - In the same else condition check if occurrence of this element is >= (array.length/2 + 1) - Break the for loop and return the element if the condition is met