There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.
n = 7
ar = [1, 2, 1, 2, 1, 3, 2]
There is one pair of color 1 and one of color 2. There are three odd socks left, one of each color. The number of pairs is 2.
Complete the sockMerchant function in the editor below.
sockMerchant has the following parameter(s):
The first line contains an integer n, the number of socks represented in ar.
The second line contains space-separated integers, ar[i], the colors of the socks in the pile.
def sockMerchant(n, ar):
# Write your code here
set_ar = set(ar)
answer = 0
for i in set_ar:
cnt = ar.count(i)
if cnt > 1:
answer += cnt//2
return answer