Number digits same frequency algorithm in JavaScript

Given two numbers such as 1987456 and 5796184, check if the two numbers have the same frequency of digits. That is, they have the same digits the same number of times. Another way of putting this would be to check if the digits of the two numbers are anagrams of each other or not.

function sameFrequency(a, b) {
  a = String(a);
  b = String(b);
  if (a.length !== b.length) {
    return false;
  }
  const freq_a = {};
  const freq_b = {};
  for (let i = 0; i < a.length; i++) {
    freq_a[a[i]] = (freq_a[a[i]] || 0) + 1;
    freq_b[b[i]] = (freq_b[b[i]] || 0) + 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$.


Tech mentioned