John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are socks with colors . There is one pair of color and one of color . There are three odd socks left, one of each color. The number of pairs is .
Answers
Answer:
sockMerchant consits following parameter:
n: number of socks in the pile
ar: colors of each sock
Input Format
First line has an integer
Second line has space-separated integers
Constraints
where
Output Format
Total number socks that John sell.
Sample Input
9
10 20 20 10 10 30 50 10 20
Sample Output
3
Step-by-step explanation:
static int sockMerchant(int n, int[] sockArray) {
int matchCount = 0;
int currentColor = 0;
int length = sockArray.length;
ArrayList<Integer> sockColorsArray = new ArrayList<>();
for(int i = 0; i < length; i++){
currentColor = sockArray[i];
if(sockColorsArray.contains(currentColor)){
matchCount++;
sockColorsArray.remove(new Integer(currentColor));
}else{
sockColorsArray.add(new Integer(currentColor));
}
}
return matchCount;
}