38 lines
975 B
C++
38 lines
975 B
C++
#include "unordered_containers.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <set>
|
|
|
|
namespace stl_containers {
|
|
|
|
auto first_unique(const std::vector<std::string>& words) -> std::string {
|
|
auto s = std::set<std::string>();
|
|
for (const auto& word : words) {
|
|
if (!s.contains(word)) {
|
|
s.insert(word);
|
|
} else {
|
|
s.erase(word);
|
|
}
|
|
}
|
|
return s.empty() ? "" : *s.begin();
|
|
}
|
|
|
|
auto group_anagrams(const std::vector<std::string>& words)
|
|
-> std::unordered_map<std::string, std::vector<std::string>> {
|
|
auto groups = std::unordered_map<std::string, std::vector<std::string>>();
|
|
for (const auto& word : words) {
|
|
auto key = word;
|
|
std::ranges::sort(key);
|
|
groups[key].push_back(word);
|
|
}
|
|
return groups;
|
|
}
|
|
|
|
auto has_duplicate(const std::vector<int>& data) -> bool {
|
|
auto s = std::set<int>{};
|
|
for (auto i : data) s.insert(i);
|
|
return s.size() != data.size();
|
|
}
|
|
|
|
} // namespace stl_containers
|