PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to design time- and space-efficient algorithms for sorted array problems, focusing on identifying a single non-duplicated element among paired values.

  • medium
  • Xiaohongshu
  • Coding & Algorithms
  • Software Engineer

Find the Unique Element in Pairs

Company: Xiaohongshu

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a sorted integer array `nums`. Every value appears exactly twice, except for one value that appears exactly once. Find and return the value that appears only once. Requirements: - The algorithm must run in `O(log n)` time. - The algorithm must use `O(1)` extra space. Example 1: ```text Input: nums = [1,1,2,3,3,4,4,8,8] Output: 2 ``` Example 2: ```text Input: nums = [3,3,7,7,10,11,11] Output: 10 ``` Constraints: - `1 <= nums.length <= 100000` - `nums.length` is odd. - The array is sorted in nondecreasing order. - Exactly one element appears once; every other element appears exactly twice.

Quick Answer: This question evaluates a candidate's ability to design time- and space-efficient algorithms for sorted array problems, focusing on identifying a single non-duplicated element among paired values.

You are given a sorted integer array `nums` where every value appears exactly twice, except for one value that appears exactly once. Find and return the single value. Your solution must run in `O(log n)` time and use `O(1)` extra space. Because the array is sorted, the duplicated values follow a pairing pattern that changes at the unique element. Use this property to search efficiently instead of scanning the whole array.

Constraints

  • 1 <= nums.length <= 100000
  • nums.length is odd
  • The array is sorted in nondecreasing order
  • Exactly one element appears once; every other element appears exactly twice

Examples

Input: ([1,1,2,3,3,4,4,8,8],)

Expected Output: 2

Explanation: All numbers appear twice except `2`, so the answer is 2.

Input: ([3,3,7,7,10,11,11],)

Expected Output: 10

Explanation: The only value that does not have a matching pair is 10.

Input: ([5],)

Expected Output: 5

Explanation: A single-element array means that element is the unique one.

Input: ([0,1,1,2,2,3,3],)

Expected Output: 0

Explanation: The unique value appears at the beginning of the array.

Input: ([1,1,2,2,9],)

Expected Output: 9

Explanation: The unique value appears at the end of the array.

Hints

  1. Before the unique element, pairs start at even indices. After the unique element, that pattern shifts.
  2. During binary search, force `mid` to be even and compare `nums[mid]` with `nums[mid + 1]`.
Last updated: Jun 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.