Javascript | Brian Kernighan’s Algorithm


  • 0
    /**
     * @param {number} n - a positive integer
     * @return {number}
     */
    var hammingWeight = function(n) {
        var count = 0;
        while (n) {
          n &= (n-1) ;
          count++;
        }
        return count;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.