-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_duplicates.cpp
39 lines (33 loc) · 1.07 KB
/
remove_duplicates.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <set>
#include <algorithm>
#include "remove_duplicates.h"
using namespace std;
template<typename StringKeyMap>
set<string> GetKeySet(const StringKeyMap& m) {
set<string> keys;
for (const auto& item: m) {
keys.insert({item.first.begin(), item.first.end()});
}
return keys;
}
template<typename StringKeyMap>
bool AreEqualByKeys(const StringKeyMap& lhs, const StringKeyMap& rhs) {
if (lhs.size() != rhs.size()) {
return false;
}
return GetKeySet(lhs) == GetKeySet(rhs);
}
void RemoveDuplicates(SearchServer& search_server) {
set<int> duplicates;
for (auto lhs = search_server.begin(); lhs != search_server.end(); ++lhs) {
for (auto rhs = next(lhs, 1); rhs != search_server.end(); ++rhs) {
if (AreEqualByKeys(search_server.GetWordFrequencies(*lhs), search_server.GetWordFrequencies(*rhs))) {
duplicates.insert(*rhs);
}
}
}
for (const int id: duplicates) {
cout << "Found duplicate document id " << id << endl;
search_server.RemoveDocument(id);
}
}