Skip to content

Instantly share code, notes, and snippets.

@johntran
Last active April 13, 2016 23:03
Show Gist options
  • Save johntran/29ca366fdcc1f8ab0efa2d7753268c48 to your computer and use it in GitHub Desktop.
Save johntran/29ca366fdcc1f8ab0efa2d7753268c48 to your computer and use it in GitHub Desktop.
// It was basically with a set of numbers in an array
// Give back a count of numbers with the most frequent numbers that are the same
// superoptimized version
var numberSet = function (set) {
var counts = {};
var highestCount = 0;
var numbers = [];
}
for(var i=0; i < set.length; i++) {
if(!counts[set[i]]) {
counts[set[i]] = 1;
} else {
counts[set[i]]++;
}
// console.log('counts:', counts,
// 'number:', set[i],
// 'counts of number:', counts[set[i]])
if (counts[set[i]] === highestCount) {
numbers.push(set[i])
}
else if(counts[set[i]] > highestCount) {
highestCount = counts[set[i]];
numbers = [set[i]]
};
// console.log('numbers:', numbers)
};
return numbers
}
//first attempt
var numberSet = function (set) {
var counts= {}
var highestCount = 0;
for(var i=0; i < set.length; i++) {
if(!counts[set[i]]) {
counts[set[i]] = 1;
} else {
counts[set[i]]++;
}
if(counts[set[i]] > highestCount) {
highestCount = counts[set[i]];
};
};
var highestNumbers = [];
for(var key in counts) {
if(counts[key] === highestCount){
highestNumbers.push(key)
}
}
return highestNumbers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment