Single Number

Easy Bit Manipulation Missing / Repeated Numbers Original on LeetCode

Single Number is a easy bit manipulation problem solved with the missing / repeated numbers pattern. The best approach, optimal (xor all), runs in O(n) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from hash set up.

Problem

In a non-empty array, every value appears twice except one. Find that one value in linear time and constant space.

Examples

Example 1

Input
nums = [4, 1, 2, 1, 2]
Output
4

Example 2

Input
nums = [7]
Output
7

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • Every element appears twice except one. O(n) time, O(1) space.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Hash setO(n)O(n)
Optimal (XOR all)O(n)O(1)

1Hash set

TimeO(n)
SpaceO(n)

Add values you see for the first time; remove them the second time. One value is left.

  1. Toggle membership in a set; return the remaining element.
class Solution {
    public int singleNumber(int[] nums) {
        Set<Integer> s = new HashSet<>();
        for (int x : nums) if (!s.add(x)) s.remove(x);
        return s.iterator().next();
    }
}

2Optimal (XOR all)

TimeO(n)
SpaceO(1)

Every pair cancels to 0 and XOR is order-independent, so the XOR of the whole array is the single value.

  1. x = 0; x ^= each value.
class Solution {
    public int singleNumber(int[] nums) {
        int x = 0;
        for (int v : nums) x ^= v;
        return x;
    }
}

Edge cases to test

  • Negative numbers
  • Single element

Hints

Hint 1

x ^ x = 0, and XOR does not care about order.

FAQ

What is the best time complexity for Single Number?

Optimal (XOR all) runs in O(n) time and O(1) extra space.

Which pattern does Single Number use?

It is a bit manipulation problem that uses the missing / repeated numbers pattern. Other problems with the same pattern: Missing Number, Two numbers with odd occurrences.

Is there a brute force solution for Single Number?

Yes. Hash set takes O(n) time and O(n) space. Add values you see for the first time; remove them the second time.

Which edge cases should I test for Single Number?

Negative numbers; Single element.