哥哥妹妹分糖果之不给糖就捣蛋的妹妹
描述:哥哥妹妹分糖果,偶数数组,一个数字代表一个种类的糖果,均分,问妹妹最多能获得的糖果种类
一、
var arr = [1,1,2,2,3,3,4,4,4,4,7,7]
var distributeCandies = function (candies) {
let len = candies.length;
let sister = [];
let kinds = '';
kinds = candies.filter(ele => {
if (sister.indexOf(ele) == -1) {
if (sister.length < len / 2) {
sister.push(ele);
return sister
}
}
})
return kinds.length
};
var res = distributeCandies(arr);
console.log(res);
分析
二、
var arr = [1, 2, 3, 4, 5, 6];
var distributeCandies = function (candies) {
return Math.min(candies.length/2,new Set(candies).size)
};
var res = distributeCandies(arr);
console.log(res);