Skip to content

Instantly share code, notes, and snippets.

@mikeyakymenko
Created September 22, 2019 15:08
Show Gist options
  • Save mikeyakymenko/c165fc8f2e9b30c498a6894ae25ea675 to your computer and use it in GitHub Desktop.
Save mikeyakymenko/c165fc8f2e9b30c498a6894ae25ea675 to your computer and use it in GitHub Desktop.
Some candies ;)
var candies1 = [3, 4, 7, 7, 6, 6]
var candies2 = [5, 1, 12, 12, 12, 5, 6, 8, 8, 5]
var candies3 = [80, 80, 1000000000, 80, 80, 80, 80, 80, 80, 123456789]
var candies = [5, 1, 12, 12, 12, 5, 6, 8, 8, 5]
var typesOfCandies = function(arr) {
var finalResult = {};
var counting = arr.reduce(function(result, current) {
result[current] = (result[current] || 0) + 1;
return result;
}, {});
finalResult.unique = Object.keys(counting);
finalResult.number = Object.keys(counting).slice(0, arr.length/2).length;
console.log(finalResult);
return finalResult.number
}
console.log(typesOfCandies(candies1));
console.log(typesOfCandies(candies2));
console.log(typesOfCandies(candies3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment