Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
Example 1:
Input: nums = [1,2,1,3,2,5] Output: [3,5] Explanation: [5, 3] is also a valid answer.
Example 2:
Input: nums = [-1,0] Output: [-1,0]
Example 3:
Input: nums = [0,1] Output: [1,0]
Constraints:
2 <= nums.length <= 3 * 104-231 <= nums[i] <= 231 - 1nums will appear twice, only two integers will appear once.Problem summary: Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[1,2,1,3,2,5]
[-1,0]
[0,1]
single-number)single-number-ii)find-the-original-array-of-prefix-xor)find-the-xor-of-numbers-which-appear-twice)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #260: Single Number III
class Solution {
public int[] singleNumber(int[] nums) {
int xs = 0;
for (int x : nums) {
xs ^= x;
}
int lb = xs & -xs;
int a = 0;
for (int x : nums) {
if ((x & lb) != 0) {
a ^= x;
}
}
int b = xs ^ a;
return new int[] {a, b};
}
}
// Accepted solution for LeetCode #260: Single Number III
func singleNumber(nums []int) []int {
xs := 0
for _, x := range nums {
xs ^= x
}
lb := xs & -xs
a := 0
for _, x := range nums {
if x&lb != 0 {
a ^= x
}
}
b := xs ^ a
return []int{a, b}
}
# Accepted solution for LeetCode #260: Single Number III
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
xs = reduce(xor, nums)
a = 0
lb = xs & -xs
for x in nums:
if x & lb:
a ^= x
b = xs ^ a
return [a, b]
// Accepted solution for LeetCode #260: Single Number III
impl Solution {
pub fn single_number(nums: Vec<i32>) -> Vec<i32> {
let xs = nums.iter().fold(0, |r, v| r ^ v);
let lb = xs & -xs;
let mut a = 0;
for x in &nums {
if (x & lb) != 0 {
a ^= x;
}
}
let b = xs ^ a;
vec![a, b]
}
}
// Accepted solution for LeetCode #260: Single Number III
function singleNumber(nums: number[]): number[] {
const xs = nums.reduce((a, b) => a ^ b);
const lb = xs & -xs;
let a = 0;
for (const x of nums) {
if (x & lb) {
a ^= x;
}
}
const b = xs ^ a;
return [a, b];
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.