Hamming distance

OSCC · JavaScript, Math, Algorithm · Dec 28, 2020

Calculates the Hamming distance between two values.

const hammingDistance = (num1, num2) =>
  ((num1 ^ num2).toString(2).match(/1/g) || '').length;
hammingDistance(2, 3); // 1

Recommended snippets