• + 0 comments

    My C++ solution

    int sockMerchant(int n, vector<int> ar) {
        std::vector<bool> odds = std::vector<bool>(100);
        int nPairs = 0;
        for ( int sockID : ar ) {
            if ( odds[sockID-1] ) {
                nPairs++;
                odds[sockID-1] = false;
            } else {
                // odd sock found
                odds[sockID-1] = true;
            }
        }
        return nPairs;
    }