How to find the duplicate words in a 2d vector of words with C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <unordered_map>

int main() {
    std::vector<std::vector<std::string>> vec = {
        {"c", "c++", "go"},
        {"java", "c", "java"},
        {"c++", "php", "javascript"}
    };

    std::unordered_map<std::string, int> freq;

    for (const auto& row : vec) {
        for (const auto& str : row) {
            freq[str]++;
            if (freq[str] > 1) {
                std::cout << "Duplicate found: " << str << std::endl;
            }
        }
    }
}



/*
run:

Duplicate found: c
Duplicate found: java
Duplicate found: c++

*/

 



answered Jul 6, 2024 by avibootz

Related questions

1 answer 113 views
1 answer 93 views
1 answer 182 views
1 answer 230 views
1 answer 116 views
...