Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • About
  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

👥10,176visitors

Built for engineers who want to go deep.

← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
  • Practice problems

    Top K Frequent Elements
  • Find Median from Data Stream
  • Kth Largest Element in an Array
  • Merge K Sorted Lists
  • Top K Frequent Words

Linked List Patterns

  • Practice problems

    Reverse Linked List
  • Linked List Cycle
  • Merge Two Sorted Lists
  • Reverse Linked List II
  • Linked List Cycle II
  • Remove Nth Node From End of List

Stack & Queue Patterns

  • Practice problems

    Valid Parentheses
  • Min Stack
  • LRU Cache
  • Daily Temperatures
  • Next Greater Element I

Recursion & Backtracking Patterns

  • Practice problems

    Subsets
  • Permutations
  • N-Queens
  • Combination Sum

Greedy Patterns

  • Practice problems

    Jump Game
  • Gas Station

Binary Search Patterns

  • Practice problems

    Binary Search
  • Search in Rotated Sorted Array
  • Find Minimum in Rotated Sorted Array

Bit Manipulation Patterns

  • Practice problems

    Single Number
  • Counting Bits
  • Number of 1 Bits

Sorting Patterns

  • Practice problems

    Merge Intervals
  • Meeting Rooms II
  • Find the Duplicate Number
  • First Missing Positive
Chaturmind
← Interview Coding Patterns

Core Patterns

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort

Heap & Priority Queue Patterns

  • Top-K Elements
  • K-Way Merge
  • Two Heaps
  • Practice problems

    Top K Frequent Elements
  • Find Median from Data Stream
  • Kth Largest Element in an Array
  • Merge K Sorted Lists
  • Top K Frequent Words

Linked List Patterns

  • Practice problems

    Reverse Linked List
  • Linked List Cycle
  • Merge Two Sorted Lists
  • Reverse Linked List II
  • Linked List Cycle II
  • Remove Nth Node From End of List

Stack & Queue Patterns

  • Practice problems

    Valid Parentheses
  • Min Stack
  • LRU Cache
  • Daily Temperatures
  • Next Greater Element I

Recursion & Backtracking Patterns

  • Practice problems

    Subsets
  • Permutations
  • N-Queens
  • Combination Sum

Greedy Patterns

  • Practice problems

    Jump Game
  • Gas Station

Binary Search Patterns

  • Practice problems

    Binary Search
  • Search in Rotated Sorted Array
  • Find Minimum in Rotated Sorted Array

Bit Manipulation Patterns

  • Practice problems

    Single Number
  • Counting Bits
  • Number of 1 Bits

Sorting Patterns

  • Practice problems

    Merge Intervals
  • Meeting Rooms II
  • Find the Duplicate Number
  • First Missing Positive
HomeLearnInterview Coding PatternsBit Manipulation Patterns
EasyBit Manipulation

Single Number

bit-manipulationxor

Problem

Given a non-empty integer array nums in which every element appears exactly twice except one, find the element that appears only once.

Input: nums = [4, 1, 2, 1, 2]   →   Output: 4
Input: nums = [2, 2, 1]         →   Output: 1
  • Your solution must run in linear time and use only constant extra space. That rules out sorting (O(n log n)) and hash sets (O(n) space) as the intended answer.
  • Values can be negative. The array length is odd.
  • Related variants: every other element appears three times (Single Number II), or two elements appear once (Single Number III). Both use bit manipulation too.

Examples

Example 1

Input: nums = [2,2,1]

Output: 1

Explanation: 2 appears twice, 1 appears once.

Example 2

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

Output: 4

Explanation: 1 and 2 each appear twice; 4 is the single element.

Constraints

  • •1 <= nums.length <= 3*10^4
  • •-3*10^7 <= nums[i] <= 3*10^7
  • •Every element appears twice except one

Hints

Hint 1

The brute force: count every number's frequency with a hash map, then report whichever has count 1 — O(n) time, but O(n) extra space.

Hint 2

The O(1)-space requirement is the real signal here — what operation, applied to every element, could cancel out anything appearing an even number of times?

Hint 3

XOR: a^a = 0 for any a, and XOR is order-independent — XORing the whole array cancels every pair, leaving only the unpaired element.

Solutions

public int singleNumberBruteForce(int[] nums) {
    Map<Integer, Integer> counts = new HashMap<>();
    for (int n : nums) counts.merge(n, 1, Integer::sum);
    for (var entry : counts.entrySet()) {
        if (entry.getValue() == 1) return entry.getKey();
    }
    throw new IllegalArgumentException("No single number found");
}

Time: O(n) · Space: O(n)

Advertisement

Previous · Practice problem

Find Minimum in Rotated Sorted Array

Next · Practice problem

Counting Bits