import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class HackerRankSockMerchant {

    private static void sockMerchant(int n, int[] ar) {
        List<Integer> arList = Arrays.stream(ar)
                .mapToObj(i->i)
                .collect(Collectors.toList());

        /*List<Integer> arList = new ArrayList<>();
        for(int arItem: ar) {
            arList.add(arItem);
        }*/
        System.out.println("arList " + arList);

        //1. Sort the list
        List<Integer> arList1 = (List<Integer>) ((ArrayList<Integer>) arList).clone();
        Collections.sort(arList1, Integer::compareTo);
        System.out.println("sorted arList1 " + arList1);

        //2. Extract all colors:
        List<Integer> colorList = new ArrayList<>();
        for(Integer i: arList1) {
            if(!colorList.contains(i)) {
                colorList.add(i);
            }
        }
        System.out.println("There are " + colorList.size() + " colors.");

        int pair = 0;
        //3. How many of each?
        for(Integer color: colorList) {
            List<Integer> cList = arList.stream()
                    .filter(i -> i.equals(color))
                    .collect(Collectors.toList());
            System.out.println("Color: " + color + " are " + cList.size());

            if (cList.size() % 2 == 0) {
                pair = pair + cList.size()/2;
            } else {
                pair = pair + (cList.size()-1)/2;
            }
        }
        System.out.println("Pairs are: " + pair);
    }

    public static void main(String[] args) throws IOException {

        int no1 = 7;
        int[] socks1 = {1,2,1,2,3,1,2};

        int no2 = 3;
        int[] socks2 = {3,3,3};

        int no3 = 4;
        int[] socks3 = {1,1,2,2};

        sockMerchant(no3, socks3);
    }


}
