Skip to content

Instantly share code, notes, and snippets.

@stephensanwo
Last active April 7, 2021 23:50
Show Gist options
  • Save stephensanwo/f986f37b9f1949640760055e82ed7e3e to your computer and use it in GitHub Desktop.
Save stephensanwo/f986f37b9f1949640760055e82ed7e3e to your computer and use it in GitHub Desktop.
Algorithms and Data Structures
# find the pairs of numbers in list ar of length n, and return the number of pairs in the list
n = 9
ar = [10, 20, 20, 10, 10, 30, 50, 10, 20]
def sockMerchant(n, ar):
i = 0
data = sorted(ar)
pair = 0
while i < len(data)-1:
if data[i] == data[i+1]:
i += 2
pair += 1
else:
i += 1
if i == len(data):
break
return pair
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment