Sock Merchant

Sock Merchant

Sock Merchant:

John’s clothing store has a pile of n loose socks where each sock i is labeled with an integer,\(c_1\) , denoting its color. He wants to sell as many socks as possible, but his customers will only buy them in matching pairs. Two socks,i and j are a single matching pair if \(c_i\) = \(c_j\)

Given n and the color of each sock, how many pairs of socks can John sell?

Input Format

The first line contains an integer, n , denoting the number of socks. The second line contains n space-separated integers describing the respective values of \(c_0\),\(c_1\),\(c_2\),..\(c_n-1\)

Output Format

Print the total number of matching pairs of socks that John can sell.

Constraints:
  • 1<=n <=100
  • 1<= \(c_i\) <=100
Sample Input
9
10 20 20 10 10 30 50 10 20

Sample Output

3

Explanation
image

As you can see from the figure above, we can match three pairs of socks. Thus, we print 3 on a new line.

Solution

#!/bin/python

import sys
import collections

n = int(raw_input().strip())
c = map(int,raw_input().strip().split(' '))
my_s=[]
for i in list(set(c)):
    my_s.append(c.count(i))
total=0
for i in my_s:
    if i >1:
        i=i/2
        total+=i
print total
comments powered by Disqus