/javascript

Finding Duplicate in JS Array

The Set object lets you store unique values of any type, whether primitive values or object references.

var containsDuplicate = function(nums) {
  let set = new Set();

  for (let n of nums) {
    if (set.has(n)) {
      return true;
    }
    set.add(n);
  }
  return false;
};

Subscribe to Necmettin Karakaya

Get the latest posts delivered right to your inbox

Necmettin Karakaya

Necmettin Karakaya

Full Stack Developer