Find Duplicates in ArrayLoading saved progress…

Find Duplicates in Array

Implement arrayFindDuplicate(nums) — given an array of numbers, return true if any value appears at least twice, and false if every element is distinct. This is the classic LeetCode "Contains Duplicate" problem: you are not asked which value repeats or how often, only whether a repeat exists at all.

Signature

// nums: number[]   — the array to inspect.
// returns: boolean  — true if some value appears two or more times,
//                     false if every element is unique.
function arrayFindDuplicate(nums): boolean;

Examples

// 1 appears at both the start and the end.
arrayFindDuplicate([1, 2, 3, 1]);
// → true
// Every element is distinct.
arrayFindDuplicate([1, 2, 3, 4]);
// → false

Notes

  • Any repeat counts. You return true the moment a single value shows up twice. You do not need to find or report the value itself.
  • Empty and single-element arrays are distinct. arrayFindDuplicate([]) and arrayFindDuplicate([7]) both return false — there is nothing to repeat.
  • Values can be negative or zero. 0 is a real value, not an empty slot; [0, 1, 0] has a duplicate.
  • Don't mutate the input. Read nums; don't sort or splice it to detect repeats.
Loading editor…