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
 
an> + 1; } for (const digit of Object.keys(freq_a)) { if (freq_a[digit] !== freq_b[digit]) { return false; } } return true; }

This algorithm is $O(n)$ where $n$ is the number of digits, as it iterates twice to a maximum of $n$.


View post: Number digits same frequency algorithm in JavaScript