Hamming distance

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

More like this

  • JavaScript Algorithms

    Learn a handful of popular algorithms, implemented in JavaScript ES6.

    Collection · 35 snippets

  • Euclidean distance

    Calculates the distance between two points in any number of dimensions.

    JavaScript, Math · Dec 28, 2020

  • Vector distance

    Calculates the distance between two vectors.

    JavaScript, Math · Dec 28, 2020

  • Distance between two points

    Calculates the distance between two points.

    JavaScript, Math · Dec 28, 2020