mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
Python file for finding number of pairs
This commit is contained in:
parent
85ee27687a
commit
88de7ccff5
44
maths/iterative_pair.py
Normal file
44
maths/iterative_pair.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
import math
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import sys
|
||||
from collections import Counter
|
||||
import doctest
|
||||
|
||||
#
|
||||
# Complete the 'sockMerchant' function below.
|
||||
#
|
||||
# The function is expected to return an INTEGER.
|
||||
# The function accepts following parameters:
|
||||
# 1. INTEGER n
|
||||
# 2. INTEGER_ARRAY ar
|
||||
#
|
||||
|
||||
|
||||
def sockMerchant(n, ar):
|
||||
"""
|
||||
>>> sockMerchant(9, [10, 20, 20, 10, 10, 30, 50, 10, 20])
|
||||
3
|
||||
>>> sockMerchant(4, [1, 1, 3, 3])
|
||||
2
|
||||
|
||||
"""
|
||||
|
||||
i = 0
|
||||
occur = Counter(ar)
|
||||
|
||||
for x in occur.values():
|
||||
i += x // 2
|
||||
return i
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
n = int(input("Enter length of array:- \n").strip())
|
||||
|
||||
ar = list(map(int, input("Enter the elements: \n").rstrip().split()))
|
||||
|
||||
result = sockMerchant(n, ar)
|
||||
print(result)
|
||||
doctest.testmod()
|
Loading…
Reference in New Issue
Block a user