Skip to main content

30. Contains Duplicate

easyAsked at Salesforce

Determine if any value appears at least twice in an array. Salesforce uses this to test reach-for-Set instincts and articulate the time-space trade-off.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses this as a dedup-check warmup for record-import flows.
  • LeetCode Discuss (2025)Common precursor before harder duplicate problems.

Problem

Given an integer array nums, return true if any value appears at least twice in the array, and return 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

Example 3

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

Approaches

1. Sort and check adjacent

Sort the array; return true if any two adjacent elements are equal.

Time
O(n log n)
Space
O(1) if in-place sort
function containsDuplicate(nums) {
  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: Mutates input and is O(n log n). Salesforce wants the linear-time set-based answer.

2. Hash set

Walk the array; if a value is already in the set, return true. Otherwise add it.

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: Short-circuits on the first duplicate. Optimal time at the cost of O(n) space.

Salesforce-specific tips

Salesforce uses dedup logic everywhere — Leads, Contacts, Accounts. They grade on whether you reach for Set within seconds. Bonus signal: also mention `return new Set(nums).size !== nums.length` as a one-liner — concise and idiomatic JS.

Common mistakes

  • Using nested loops for O(n^2) — slow on 10^5 elements.
  • Sorting the input without realizing it mutates — fine for this problem but worth calling out.
  • Using an object {} with number keys that exceed Number.MAX_SAFE_INTEGER — Set is safer.

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • Contains Duplicate II (LC 219) — duplicates within distance k.
  • Contains Duplicate III (LC 220) — values within absolute difference t at distance k.
  • Find all duplicates in O(n) time and O(1) extra space (LC 442).

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why use Set instead of an object?

Set handles arbitrary values cleanly (no string coercion). Object keys are coerced to strings, which causes issues with numbers that round (e.g., 10^16).

What if I'm constrained to O(1) space?

If values are in a bounded range [0, n-1], you can use the array itself (flip signs to mark visited). Otherwise sort-then-scan is the O(1)-space solution.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →