Skip to content

Latest commit

 

History

History
30 lines (26 loc) · 720 Bytes

136. Single Number.md

File metadata and controls

30 lines (26 loc) · 720 Bytes

136. Single Number

Solution 1

  • xor
class Solution {
    public int singleNumber(int[] nums) {
        //there are two same elemets and find the unique one
        //Using xor: same elemet will be 0, different will be one
        //and further more aa^a^b  = b
        int res = 0;
        for(int num : nums){
            res ^= num;
        }
        return res;
    }
}

Solution 2

  • using hahet, push unmet element and pop same elemnt, then only unique element left

Accepted

Runtime: 0 ms, faster than 100.00% of Java online submissions for Single Number.
Memory Usage: 39.2 MB, less than 81.48% of Java online submissions for Single Number.

Similiar

  • Airbnb question