Accepted Java solution based on HashSet.


  • 0
    O
    public class Solution {
        public int[] singleNumber(int[] nums) {
            if(nums.length <= 2) {
                return nums;
            }
            HashSet<Integer> hSet = new HashSet<Integer>();
            for(int val: nums) {
                if (hSet.contains(val)) {
                    hSet.remove(val);
                } else {
                    hSet.add(val);
                }
            }
            Iterator iterator = hSet.iterator();
            int[] result = new int[2];
            result[0] = (int)iterator.next();
            result[1] = (int)iterator.next();
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.