N-Repeated Element in Size 2N Array

You are given an integer array nums of length 2 * n. It contains n + 1 unique elements, and exactly one of them is repeated n times. Return the element that is repeated n times.

30:00

N-Repeated Element in Size 2N Array
easy
Free written solution
Topics
Companies

You are given an integer array nums of length 2 * n. It contains n + 1 unique elements, and exactly one of them is repeated n times. Return the element that is repeated n times.

Example 1:
Input: {"nums":[1,2,3,3]}
Output: 3
Constraints:
  • 2≤n≤50002 \leq n \leq 5000

  • nums.length==2n\text{nums.length} == 2n

  • 0≤nums[i]≤1040 \leq \text{nums}[i] \leq 10^4

  • nums contains n + 1 unique elements and one of them is repeated exactly n times.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,3,3]}

Half of the 4 slots hold the same value, so two copies must sit within 3 positions of each other somewhere. Try gaps 1, 2 and 3.

gap 1

pairs checked: 0

gap 2

pairs checked: 0

gap 3

pairs checked: 0

10213233

Some run of 4 consecutive slots must hold two copies of the repeated value.

Variables
No variables to display
DepthFunction Call
Stack empty
0/5