Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 486 Bytes

Single Number.md

File metadata and controls

30 lines (22 loc) · 486 Bytes

Notes

uses XOR. Keep in mind:

  1. x ^ x = 0
  2. x ^ 0 = x
  3. XOR is commutative and associative

Solution

class Solution {
    public int singleNumber(int[] array) {
        int result = 0;
        for (int num : array) {
            result = result ^ num; // ^ is XOR operator
        }
        return result;
    }
}

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Links