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++){
count[ar[i]]++;
}
int result = 0;
for(auto it=count.begin();it!=count.end();it++){
result += (it->second/2);
}
return result;
}
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++){
count[ar[i]]++;
}
int result = 0;
for(auto it=count.begin();it!=count.end();it++){
result += (it->second/2);
}
return result;
}
This is a great idea and thanks for sharing