Created
September 11, 2016 23:37
-
-
Save dmjcomdem/b8edd46b3633c65190c3ece420a5800d to your computer and use it in GitHub Desktop.
нахождение количество повторений в числовом массиве
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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