N-Repeated Element in Size 2N Array — LeetCode 961 Python Solution

EasyArrayHash Table
Problem
#961
Pattern
Hash Map
Reading time
2 min

The problem

You are given an integer array nums with the following properties: nums.length == 2 * n. nums contains n + 1 unique elements.

Example

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

Python solution

Python
class Solution:
    def repeatedNTimes(self, nums: List[int]) -> int:
        s = set()
        for x in nums:
            if x in s:
                return x
            s.add(x)

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 961. N-Repeated Element in Size 2N Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 961. N-Repeated Element in Size 2N Array?
LeetCode 961. N-Repeated Element in Size 2N Array is rated Easy on LeetCode.
What is the time complexity of LeetCode 961. N-Repeated Element in Size 2N Array?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 961. N-Repeated Element in Size 2N Array?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 961. N-Repeated Element in Size 2N Array cover?
LeetCode 961. N-Repeated Element in Size 2N Array is tagged Array and Hash Table on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview