Skip to main content

5. Contains Duplicate

easyAsked at Confluent

Return true if any value appears at least twice — Confluent uses it to check if you reach for a Set immediately, the same instinct used to dedupe Kafka messages.

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

Problem

Given an integer array, 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. Sort and scan

Sort then check adjacent equal pairs.

Time
O(n log n)
Space
O(1)
nums.sort((a,b)=>a-b);
for (let i=1;i<nums.length;i++)
  if (nums[i]===nums[i-1]) return true;
return false;

Tradeoff:

2. Hash set

Walk once, return true on the first repeat seen in the set, else false at the end.

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

Tradeoff:

Confluent-specific tips

Confluent will pivot to streaming dedupe — show you understand that exactly-once semantics let you swap a Set for a state store keyed by partition without re-emitting on rebalance.

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 Confluent interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →