Skip to content

Instantly share code, notes, and snippets.

@erosenberg
Created October 1, 2017 17:09
Show Gist options
  • Save erosenberg/a133b484fcc426b10ab2133bf1bedff8 to your computer and use it in GitHub Desktop.
Save erosenberg/a133b484fcc426b10ab2133bf1bedff8 to your computer and use it in GitHub Desktop.
HashTable in ES6 for Leetcode #575
const distributeCandies = function(candies) {
if (candies.length % 2 !== 0) return candies;
candies = candies.sort(function(a, b) { return a - b; });
const half = candies.length / 2;
let dict = new Map();
let maxValue = Object.keys(dict).length;
candies.forEach((candy, i) => {
if (!dict.has(candy)) {
dict.set(candy, 1);
} else {
dict.set(candy, dict.get(candy) + 1);
}
});
if (dict.size > half) {
return half;
}
return dict.size;
};
const input = [1, 1, 2, 3];
const sistersMax = distributeCandies(input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment