Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 557 Bytes

sock-merchant.MD

File metadata and controls

25 lines (21 loc) · 557 Bytes

Sock Merchant (HackerRank)

https://www.hackerrank.com/challenges/sock-merchant


// Complete the sockMerchant function below.
int sockMerchant(int n, vector<int> ar) {
    unordered_map<int, int> socks_counter;
    for (auto &sock: ar) {
        if (socks_counter.find(sock) != socks_counter.end()) {
            socks_counter.at(sock)++;
        } else {
            socks_counter.emplace(sock, 1);
        }
    }

    int count = 0;
    for (auto &socks: socks_counter) {
        count += socks.second / 2;
    }

    return count;
}