• + 2 comments

    Here is the equivelent JavaScript code with comments:

    function pickingNumbers(a) {
        return a.reduce((count,val) => {
            // count occurance of each number
            count[val]++
            return count;
        }, new Array(100).fill(0))
            .reduce((max, val, index, count) =>{
            // max number of integers such that the absolute 
            // difference between any two is 1
            if (max < val + count[index + 1]){
                    max = val + count[index + 1];
            }
            return max;
        }, 0);
    }
    
    1. Fill an array with zeros
    2. Increment the count for each number found
    3. Find the maximum pair of numbers

    It should be noted that you could end up with a subset containing all the same integer.