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.
- Sales by Match
- Discussions
Sales by Match
Sales by Match
Sort by
recency
|
215 Discussions
|
Please Login in order to post a comment
count each element divided by 2 note: list.count(ele) is expensive operation
def sockMerchant(n, ar):
just because, there are no many sock types. o(n)
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
C#:
golang