N-Repeated Element in Size 2N Array

EasyArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array nums of even length 2 * n. The array holds exactly n + 1 distinct values: one of them appears exactly n times, and each of the other n values appears exactly once. Return the value that appears n times. The input is guaranteed to have this shape, so the answer always exists and is unique. Aim for O(n) time; an O(1)-extra-space solution is possible.

Examples

Input: nums = [1,2,3,3]
Output: 3
Here `n = 2` and `3` is the value that shows up twice.
Input: nums = [2,1,2,5,3,2]
Output: 2
Here `n = 3` and `2` appears three times.
Input: nums = [5,1,5,2,5,3,5,4]
Output: 5
Half of the eight entries are `5`.

Constraints

  • 2 <= n <= 5000
  • nums.length == 2 * n
  • 0 <= nums[i] <= 10^4
  • `nums` contains exactly `n + 1` distinct values, one of which appears exactly `n` times

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems