Find the element repeated n times in an array of length 2n

Quick Overview

Given an array of length 2n that holds n + 1 distinct values, one of which appears n times, return the value that repeats. It tests hash set duplicate detection, reasoning from the counting guarantee in the input, and finding a version that uses constant extra space.

Find the element repeated n times in an array of length 2n

Company: Akamai

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an integer array `nums` with the following properties: - `nums.length == 2 * n`. - `nums` contains `n + 1` unique elements. - Exactly one element of `nums` is repeated `n` times. Return the element that is repeated `n` times. ### Function Signature ```python def repeated_n_times(nums: list[int]) -> int: ``` ### Rules - The input always satisfies the properties above. Every value other than the repeated one appears exactly once, so the answer is unique. ### Constraints - `2 <= n <= 5000` - `len(nums) == 2 * n` - `0 <= nums[i] <= 10^4` - `nums` contains `n + 1` unique elements and one of them is repeated `n` times. ### Examples ```text Input: nums = [1, 2, 3, 3] Output: 3 ``` Here n = 2. The element 3 appears 2 times in an array of length 4, while 1 and 2 appear once each. ```text Input: nums = [2, 1, 2, 5, 3, 2] Output: 2 ``` Here n = 3. The element 2 appears 3 times in an array of length 6, and 1, 5 and 3 appear once each. ```text Input: nums = [9, 5, 6, 9] Output: 9 ``` Here n = 2. The two copies of 9 sit at opposite ends of the array, so no two copies are adjacent.

Overview: Given an array of length 2n that holds n + 1 distinct values, one of which appears n times, return the value that repeats. It tests hash set duplicate detection, reasoning from the counting guarantee in the input, and finding a version that uses constant extra space.

Akamai logo
Akamai
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array nums with the following properties:

  • nums.length == 2 * n .
  • nums contains n + 1 unique elements.
  • Exactly one element of nums is repeated n times.

Return the element that is repeated n times.

Function Signature

def repeated_n_times(nums: list[int]) -> int:

Rules

  • The input always satisfies the properties above. Every value other than the repeated one appears exactly once, so the answer is unique.

Constraints

  • 2 <= n <= 5000
  • len(nums) == 2 * n
  • 0 <= nums[i] <= 10^4
  • nums contains n + 1 unique elements and one of them is repeated n times.

Examples

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

Here n = 2. The element 3 appears 2 times in an array of length 4, while 1 and 2 appear once each.

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

Here n = 3. The element 2 appears 3 times in an array of length 6, and 1, 5 and 3 appear once each.

Input: nums = [9, 5, 6, 9]
Output: 9

Here n = 2. The two copies of 9 sit at opposite ends of the array, so no two copies are adjacent.