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:
There are three different kinds of candies (1, 2 and 3), and two candies for each kind.
Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too. 
The sister has three different kinds of candies.

Example 2:

Input: candies = [1,1,2,3]
Output: 2
Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1]. 
The sister has two different kinds of candies, the brother has only one kind of candies.

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].


思路:记录糖果的种类,放在set集合中,然后取糖果种类和数组一半长度中较小的一个; 

package com.billkang;

import java.util.HashSet;
import java.util.Set;

/**
 * @author binkang
 * @date May 20, 2017
 */
public class DistributeCandies {
	public int distributeCandies(int[] candies) {
		Set<Integer> s = new HashSet<Integer>();
		for(int i=0;i<candies.length;i++) {
			if(!s.contains(candies[i])) {
				s.add(candies[i]);
			}
		}
		return s.size()<(candies.length/2) ? s.size():(candies.length/2);
	}
}


但是这种方法需要额外的存储空间。 如果不想用额外的set集合来存储糖果种类,可以先对数组排序,然后用一个整数来存储糖果种类。

/**
	 * 不需要额外的存储空间
	 * @param candies
	 * @return
	 */
	public int distributeCandies2(int[] candies) {
		Arrays.sort(candies);
		int res = 0;
		for(int i=0;i<candies.length;i++) {
			if(i==0 || candies[i]!=candies[i-1]) {
				res++;
			}
		}
		return Math.min(res, candies.length/2);
	}