Javascript array_count_values function
Your first function just gives an array of counts, without anyway to tell what each of them are counting.

Your second function is better, as the index shows what's being counted. You can use the index to see if it already exists, which means you don't have to sort your array.

if( a[arr[i]] )


This works fine if all values are positive integers. But what if you had strings? It would still work, but you couldn't loop through the result easily. You might also run into problems if you have values equal to existing array properties like length.

Instead you can use a Map, which is similar to the associative array in php.

function array_count_values(arr) {
    let a = new Map();
    for ( let i = 0; i &lt; arr.length; i++ ) {
        if ( a.has(arr[i]) ) {
            a.set(arr[i], a.get(arr[i])+1)
        } else {
            a.set(arr[i], 1)
        }
    }
    return a;
}