You are viewing a single comment's thread. Return to all comments →
Java using two hash sets which operations are O(1), therefore it is only needed to iterate whole array once.
public static int lonelyinteger(List<Integer> arr) { HashSet<Integer> set = new HashSet<>(); HashSet<Integer> uniqueSet = new HashSet<>(); Integer unique = arr.get(0); Integer lastUnique = arr.get(0); for(Integer element: arr){ if(!set.contains(element)){ set.add(element); uniqueSet.add(element); }else{ uniqueSet.remove(element); } } return uniqueSet.stream().findFirst().get(); }
Seems like cookies are disabled on this browser, please enable them to open this website
Lonely Integer
You are viewing a single comment's thread. Return to all comments →
Java using two hash sets which operations are O(1), therefore it is only needed to iterate whole array once.