Single Number
Single Number is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Bit Manipulation Patterns pattern, in the Bitwise XOR - Finding Single/Missing Number subpattern.
Problem statement
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input: nums = [2,2,1]
Output: 1
Example 2:
Input: nums = [4,1,2,1,2]
Output: 4
Example 3:
Input: nums = [1]
Output: 1
Constraints:
- 1 <= nums.length <= 3 104
- -3 104 <= nums[i] <= 3 104
- Each element in the array appears twice except for one element which appears only once.
Topics and companies
Topics: Array, Bit Manipulation.
Reported in interviews at Google, Amazon, Facebook, Microsoft.
How to practise Single Number on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Single Number in the code editor, or read the Single Number editorial for a worked solution with its approach and complexity analysis.
Where Single Number sits in the DSA pattern sheet
- Bit Manipulation Patterns pattern guide
- Bitwise XOR - Finding Single/Missing Number subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Single Number
Other problems that use the same Bitwise XOR - Finding Single/Missing Number and Bit Manipulation Patterns techniques:
- Find the Difference — Easy, same subpattern
- Missing Number — Easy, same subpattern
- Single Number II — Medium, same subpattern
- Counting Bits — Easy, same pattern
- Number of 1 Bits — Easy, same pattern
- Power of Four — Easy, same pattern
Preparing your workspace
Progress is saved as you study.