We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Easy Python with hashmap(or dict, whatever you python people call it), map all ints to how many times they occur then add up each number of occurrences divided by 2 (floored) and thats how many matching pairs:
def sockMerchant(n, ar):
# Write your code here
hm = {}
for i in ar:
if i not in hm:
hm[i] = 1
else:
val = hm.get(i)
val += 1
hm[i] = val
pairs = 0
for val in hm.values():
pairs += val//2
return pairs
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sales by Match
You are viewing a single comment's thread. Return to all comments →
Easy Python with hashmap(or dict, whatever you python people call it), map all ints to how many times they occur then add up each number of occurrences divided by 2 (floored) and thats how many matching pairs:
def sockMerchant(n, ar): # Write your code here hm = {} for i in ar: if i not in hm: hm[i] = 1 else: val = hm.get(i) val += 1 hm[i] = val pairs = 0 for val in hm.values(): pairs += val//2 return pairs