DEV Community


Discussion on: How to Solve Sock Merchant Code Challenge

saitejach127 profile image
Sai Teja

Great Article. Another approach to solving the problem would be to store the number of occurrences of each element in the array in a Hashmap and traverse the hashmap and add the count/2 to result of each element in the array.

int sockMerchant(int n, vector<int> ar) {
unordered_map<int,int> count;
for(int i=0;i<ar.size();i++){
int result = 0;
for(auto it=count.begin();it!=count.end();it++){
result += (it->second/2);
return result;

nomadkitty profile image
Jojo Zhang Author

This is a great idea and thanks for sharing