Duplicate detection algorithm in JavaScript

Here’s a super simple duplicate detection algorithm in JavaScript:

function hasDuplicate(items) {
  const seen = {};
  for (item of items) {
    if (item in seen) {
      return true;
    }
    seen[item] = true;
  }
  return false;
}

This algorithm is $O(n)$ time and $O(n)$ space.


View post: Duplicate detection algorithm in JavaScript