MathWorks Interview Question

Find the most frequent two elements in a array?

Interview Answer

Anonymous

Sep 12, 2017

Using hashmap to loop the array once. Then get the number of each element. Then use proirityqueue to loop the hashmap. If the size of the priorityqueue is larger than 2, poll the queue. When the hashmap is looped once. The two elements in the queue are the two most frequent.