How to return recurring elements in a Javascript Array with O(n) time