Menu

gitpiper

hammingDistance javascript Code Snippet in 2022

mathalgorithmintermediate

Last Updated: 25 September 2022

Calculates the Hamming distance between two values.

  • Use the XOR operator (^) to find the bit difference between the two numbers.
  • Convert to a binary string using Number.prototype.toString(2).
  • Count and return the number of 1s 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

javascript snippet similar to hammingDistance For You in September 2022

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2022 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️