Created
October 1, 2017 17:09
-
-
Save erosenberg/a133b484fcc426b10ab2133bf1bedff8 to your computer and use it in GitHub Desktop.
HashTable in ES6 for Leetcode #575
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
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