Skip to main content

217. Contains Duplicate

easyAsked at Bloomberg

Bloomberg's data-ingestion pipelines must flag duplicate trade confirmations before they corrupt position ledgers — this warmup problem tests whether you instinctively reach for a hash set instead of nested loops when deduplication speed matters.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Example 1

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

Example 2

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

Approaches

1. Brute force

Compare every pair of elements. Simple but O(n^2) — unacceptable at Bloomberg's data volumes.

Time
O(n^2)
Space
O(1)
function containsDuplicate(nums) {
  for (let i = 0; i < nums.length; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] === nums[j]) return true;
    }
  }
  return false;
}

Tradeoff:

2. Hash set single pass

Insert each element into a Set. If it already exists, a duplicate is found. Single pass, O(1) amortized lookup.

Time
O(n)
Space
O(n)
function containsDuplicate(nums) {
  const seen = new Set();
  for (const n of nums) {
    if (seen.has(n)) return true;
    seen.add(n);
  }
  return false;
}

Tradeoff:

Bloomberg-specific tips

Bloomberg uses this as a screening warmup — they want to see you arrive at the hash-set solution immediately and articulate the O(n) / O(n) tradeoff in one sentence. The follow-up is usually 'what if you can't afford the extra space?', which leads to sorting in O(n log n) / O(1). Have that answer ready; the problem itself is trivial, but the tradeoff discussion is where Bloomberg measures engineering maturity.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Contains Duplicate and other Bloomberg interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →