Recover the Value Left after Pair Cancellation

Quick Overview

Recover the sole unpaired integer by XOR cancellation in linear time and constant extra space.

Recover the Value Left after Pair Cancellation

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem In a nonempty integer array, every distinct value occurs exactly twice except one value that occurs once. Return the single-occurrence value. Use linear time and constant extra space. ```cpp int singleNumber(vector<int>& nums); ``` ### Examples ```text Input: nums = [-7,4,-7,4,9] Output: 9 ``` ```text Input: nums = [0] Output: 0 ``` ### Constraints - Length is between 1 and 30000. - Values are between -30000 and 30000, inclusive. - Exactly one value occurs once and every other value occurs twice.

Overview: Recover the sole unpaired integer by XOR cancellation in linear time and constant extra space.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
2
0

Problem

In a nonempty integer array, every distinct value occurs exactly twice except one value that occurs once. Return the single-occurrence value. Use linear time and constant extra space.

int singleNumber(vector<int>& nums);

Examples

Input: nums = [-7,4,-7,4,9]
Output: 9
Input: nums = [0]
Output: 0

Constraints

  • Length is between 1 and 30000.
  • Values are between -30000 and 30000, inclusive.
  • Exactly one value occurs once and every other value occurs twice.