Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 942 Bytes

Question_136.md

File metadata and controls

43 lines (36 loc) · 942 Bytes

LeetCode Records - Question 136 Single Number

Attempt 1: Use a HashMap

class Solution {
    public int singleNumber(int[] nums) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            Integer curr = hashMap.get(nums[i]);
            if (curr == null) {
                hashMap.put(nums[i], 1);
            } else {
                hashMap.remove(nums[i]);
            }
        }

        return hashMap.keySet().iterator().next();
    }
}
  • Runtime: 14 ms (Beats: 15.51%)
  • Memory: 44.84 MB (Beats: 91.92%)

Attempt 2: Use xor operator

class Solution {
    public int singleNumber(int[] nums) {
        int result = 0;

        for (int i = 0; i < nums.length; i++) {
            result ^= nums[i];
        }

        return result;
    }
}
  • Runtime: 1 ms (Beats: 99.85%)
  • Memory: 46.02 MB (Beats: 44.90%)