Skip to content

Instantly share code, notes, and snippets.

@dmjcomdem
Created September 11, 2016 23:37
Show Gist options
  • Save dmjcomdem/b8edd46b3633c65190c3ece420a5800d to your computer and use it in GitHub Desktop.
Save dmjcomdem/b8edd46b3633c65190c3ece420a5800d to your computer and use it in GitHub Desktop.
нахождение количество повторений в числовом массиве
var arr = [3, 7, 7, 7, 7,7, 10, 10, 8, 5, 5, 5, 5, 20, 20, 1];
function min_most_duplicate (arr) {
var result = {},
max = 0,
res;
arr = arr.slice(0); // make a copy of the array
arr.sort(function(a, b) { return (a - b); }); // sort it numerically
console.log(arr);
for( var i = 0, total = arr.length; i < total; ++i ) {
var val = arr[i],
inc = ( result[val] || 0 ) + 1;
result[val] = inc;
if( inc > max ) {
max = inc;
res = val;
}
}
return res + ' ' + result[res];
}
console.log( min_most_duplicate (arr) ); // returns 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment