Dataford
Interview Guides
Upgrade
All questions/Coding/Optimize Duplicate Detection in Arrays

Optimize Duplicate Detection in Arrays

Easy
Coding
Asked at 2 companies2SortingSearchingGreedy
Also asked at
Bain &G

Problem

Problem

At Stripe, a service receives a list of integer event IDs and needs to quickly determine whether any ID appears more than once. A naive solution compares every pair of elements, but this is too slow for large inputs. Write an optimized algorithm to detect duplicates.

Formal Specification

Implement a function contains_duplicate(nums) that takes a list of integers nums and returns a boolean:

  • True if any value appears at least twice
  • False if all values are distinct

Examples

Example 1

  • Input: nums = [1, 2, 3, 1]
  • Output: True
  • Explanation: The value 1 appears twice.

Example 2

  • Input: nums = [4, 5, 6, 7]
  • Output: False
  • Explanation: Every element is unique.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Aim to improve on the naive O(n^2) pairwise comparison approach

Your solution should focus on performance optimization and clearly use a more efficient data structure or strategy than checking all pairs.

Examples

Example 1
Inputnums = [1, 2, 3, 1]OutputTrueWhyThe value `1` appears at indices 0 and 3, so the array contains a duplicate.
Example 2
Inputnums = [4, 5, 6, 7]OutputFalseWhyNo value repeats, so all elements are distinct.
Example 3
Inputnums = [0, -1, 2, -1]OutputTrueWhyThe value `-1` appears twice.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Input contains integers only
  • Target solution should improve on O(n^2) brute force

Function Signature

def contains_duplicate(nums):

Problem

Problem

At Stripe, a service receives a list of integer event IDs and needs to quickly determine whether any ID appears more than once. A naive solution compares every pair of elements, but this is too slow for large inputs. Write an optimized algorithm to detect duplicates.

Formal Specification

Implement a function contains_duplicate(nums) that takes a list of integers nums and returns a boolean:

  • True if any value appears at least twice
  • False if all values are distinct

Examples

Example 1

  • Input: nums = [1, 2, 3, 1]
  • Output: True
  • Explanation: The value 1 appears twice.

Example 2

  • Input: nums = [4, 5, 6, 7]
  • Output: False
  • Explanation: Every element is unique.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Aim to improve on the naive O(n^2) pairwise comparison approach

Your solution should focus on performance optimization and clearly use a more efficient data structure or strategy than checking all pairs.

Examples

Example 1
Inputnums = [1, 2, 3, 1]OutputTrueWhyThe value `1` appears at indices 0 and 3, so the array contains a duplicate.
Example 2
Inputnums = [4, 5, 6, 7]OutputFalseWhyNo value repeats, so all elements are distinct.
Example 3
Inputnums = [0, -1, 2, -1]OutputTrueWhyThe value `-1` appears twice.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Input contains integers only
  • Target solution should improve on O(n^2) brute force

Function Signature

def contains_duplicate(nums):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
xAIOptimize Duplicate Pair DetectionMediumALongest Consecutive Sequence LengthMediumFind Distinct Elements in Array with CountsMedium
Next question