Find Every Value That Appears Twice

Quick Overview

Practice finding all twice-occurring values under a bounded value range, linear-time requirement, and constant auxiliary-space limit. Track duplicates correctly and account for input mutation.

Find Every Value That Appears Twice

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Find Every Value That Appears Twice An integer array `nums` has length `n`, all its values lie in `[1,n]`, and every occurring value appears either once or twice. Return all values that appear twice, each listed once. Your algorithm must take `O(n)` time and use constant auxiliary space, excluding the returned result. ### Function `vector<int> findDuplicates(vector<int>& nums)` ### Example 1 ```text Input: nums = [2,5,2,1,5] Output: [2,5] ``` Both `2` and `5` occur twice; `1` occurs once. ### Example 2 ```text Input: nums = [3,1,4,2] Output: [] ``` Every value occurs once. ### Constraints - `n == nums.length` and `1 <= n <= 100000`. - `1 <= nums[i] <= n`. - Every value present appears once or twice. ### Clarifications Return values rather than occurrence positions. The value range is part of the contract, and not every number in `[1,n]` must be present. An auxiliary set whose size grows with the input does not meet the stated space bound. ### What a Strong Answer Covers - Values present once or twice, including values at the range endpoints. - Why every duplicate is identified exactly once. - How the method meets the required time and auxiliary-space bounds. - Any temporary or permanent effects on the supplied array.

Overview: Practice finding all twice-occurring values under a bounded value range, linear-time requirement, and constant auxiliary-space limit. Track duplicates correctly and account for input mutation.

Google logo
Google
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Find Every Value That Appears Twice

An integer array nums has length n, all its values lie in [1,n], and every occurring value appears either once or twice. Return all values that appear twice, each listed once.

Your algorithm must take O(n) time and use constant auxiliary space, excluding the returned result.

Function

vector<int> findDuplicates(vector<int>& nums)

Example 1

Input: nums = [2,5,2,1,5]
Output: [2,5]

Both 2 and 5 occur twice; 1 occurs once.

Example 2

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

Every value occurs once.

Constraints

  • n == nums.length and 1 <= n <= 100000 .
  • 1 <= nums[i] <= n .
  • Every value present appears once or twice.

Clarifications

Return values rather than occurrence positions. The value range is part of the contract, and not every number in [1,n] must be present. An auxiliary set whose size grows with the input does not meet the stated space bound.

What a Strong Answer Covers Guidance

  • Values present once or twice, including values at the range endpoints.
  • Why every duplicate is identified exactly once.
  • How the method meets the required time and auxiliary-space bounds.
  • Any temporary or permanent effects on the supplied array.