Skip to main content

11. Contains Duplicate

easyAsked at Wise

Detect any duplicate value in an array — Wise uses this as a warm-up for double-debit detection in payment idempotency tests.

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

Problem

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

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Return as soon as a duplicate is found

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 nums then compare adjacent 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 early exit

Insert each value into a Set; return true the moment a value is already present. One linear pass, exits early on hit.

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:

Wise-specific tips

Wise grades whether you treat duplicates as a money-safety concern — they want the early-exit Set answer because double-charge detection in their idempotency layer follows the same shape.

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

Practice these live with InterviewChamp.AI →