^
) to find the bit difference between the two numbers.Number.prototype.toString(2)
.1
s in the string, using String.prototype.match(/1/g)
.const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length;
hammingDistance(2, 3); // 1
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️