function getCount (ARR, Rank, ranktype) { var obj = {}, K, of arr1 = []; for ( var I = 0, len = arr.length; I <len; I ++ ) { K = ARR [I]; IF (obj [K]) obj [K] ++ ; the else obj [K] =. 1 ; } // save the results {el- 'element', count- occurrences} for ( var O in obj) { arr1.push ({EL: O, COUNT: obj [O]}); } // (descending) arr1.sort ( function(N1, N2) { return n2.count - n1.count }); // if ranktype 1, for the ascending order, the array inverted IF (ranktype === 1 ) { of arr1 = arr1.reverse (); } var Rank = || Rank1 arr1.length; return arr1.slice (0 , Rank1); } getCount ([ 1,2,3,1,2,5,2,4,1,2,6,2,1,3 , 2]) // default, returns the number of all the elements appear getCount ([1,2,3,1,2,5,2,4,1,2,6,2,1,3,2], 3 ) // parameter passing (rank = 3), only returns three occurrences of sorting getCount ([1,2,3,1,2,5,2,4,1,2,6,2,1,3, 2], null ,. 1) // parameter passing (ranktype = 1, rank = null ), returns all elements of the number of occurrences in ascending getCount ([1,2,3,1,2,5,2,4,1,2,6,2,1,3,2], 3,1) // transmission parameters (rank = 3, ranktype = 1 ), only returns the number of the sort appears (in ascending order) in the first three