Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds

Example 1:


Input: candies = [1,1,2,2,3,3] Output: 3 Explanation:



Example 2:


Input: candies = [1,1,2,3] Output: 2 Explanation:



Note:

  1. The length of the given array is in range [2, 10,000], and will be even.
  2. The number in given array is in range [-100,000, 100,000].


思路:统计数组中,不一样数字的个数,然后和数组的长度的一半相比,返回两者较小值。

public class Solution {
public int distributeCandies(int[] candies) {
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < candies.length; i++) {
set.add(candies[i]);
}
return Math.min(set.size(), candies.length / 2);
}
}