day01_Sock Merchant

Sock Merchant

Function Description

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

Input Format

The first line contains an integer n , the number of socks represented in ar
The second line contains n space-separated integers describing the colors ar[i]  of the socks in the pile.

Constraints

 

Output Format

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

Sample Input

9
10 20 20 10 10 30 50 10 20

Sample Output

3

Explanation

sock.png

John can match three pairs of socks.

from collections import Counter


# Complete the sockMerchant function below.
def sockMerchant(n, ar):
    sum = 0
    for values in Counter(ar).values():
        sum += values // 2
    return sum


if __name__ == '__main__':
    n = int(input())
    ar = list(map(int, input().rstrip().split()))
    result = sockMerchant(n, ar)
    print(result)

 

posted @ 2019-07-20 14:42  乱世有歌舞  阅读(258)  评论(0编辑  收藏  举报