Check whether an integer array contains a duplicate value

Quick Overview

Decide whether an integer array contains any value that appears more than once, returning a boolean. It tests picking a data structure with fast membership checks, a single pass with an early exit, and reasoning about time and space complexity.

Check whether an integer array contains a duplicate value

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums`, determine whether any value appears more than once. Return `True` if some value appears at least twice, and `False` if every element is distinct. Aim for a solution that runs in `O(n)` time and `O(n)` extra space. ### Function Signature ```python def has_duplicate(nums: list[int]) -> bool: ``` ### Constraints - `2 <= len(nums) <= 1000` - `-10^7 <= nums[i] <= 10^7` ### Examples ```text Input: nums = [1, 1, 2, 3] Output: True ``` The value 1 appears at index 0 and again at index 1. ```text Input: nums = [1, 2, 3, 4] Output: False ``` All four values are different, so nothing repeats. ```text Input: nums = [3, -1, 7, -1] Output: True ``` The value -1 appears at index 1 and at index 3. The two copies do not have to be next to each other.

Overview: Decide whether an integer array contains any value that appears more than once, returning a boolean. It tests picking a data structure with fast membership checks, a single pass with an early exit, and reasoning about time and space complexity.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, determine whether any value appears more than once. Return True if some value appears at least twice, and False if every element is distinct.

Aim for a solution that runs in O(n) time and O(n) extra space.

Function Signature

def has_duplicate(nums: list[int]) -> bool:

Constraints

  • 2 <= len(nums) <= 1000
  • -10^7 <= nums[i] <= 10^7

Examples

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

The value 1 appears at index 0 and again at index 1.

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

All four values are different, so nothing repeats.

Input: nums = [3, -1, 7, -1]
Output: True

The value -1 appears at index 1 and at index 3. The two copies do not have to be next to each other.