DEV Community

Shivam Seth
Shivam Seth

Posted on • Updated on

Complete the sockMerchant function with python.

Alex works at a clothing store. There is a large pile of socks that must be paired 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.
Complete the sockMerchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.

sockMerchant has the following parameter(s):

n: the number of socks in the pile
ar: the colors of each sock

Complete the sockMerchant function below.

def sockMerchant(n, ar):
pears = 0
color = set()
for i in range(len(ar)):
if ar[i] not in color:
color.add(ar[i])
else:
pears += 1
color.remove(ar[i])
return pears
if name == 'main':

n = int(input())

ar = list(map(int, input().rstrip().split()))
ar.sort()


result = sockMerchant(n, ar)
print(result)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)