Reputation: 11
I am trying to read in a wordlist of 50.000 words i have and sort out all words with duplicate letters. I have already managed to select a random word, convert it to chars in an array, but how do i search that array for duplicates?
Upvotes: 0
Views: 619
Reputation:
I guess that you have an array of char pointers because of saying "convert it to chars in an array" so code would be like:
#include <iostream>
typedef const char* str;
str array [] = {"Hello", "how", "are", "you"};
bool isDuplicated (str word, str* array, int dimension);
int main() {
int length = sizeof(array) / sizeof (char);
str word = "Hello";
std::cout << "The word " << word << " is duplicated: " << isDuplicated (word, array, length) << std::endl;
std::cin.get();
}
bool isDuplicated(str word, str* array, int dimension) {
bool duplicated = false;
for(int i = 0; i < dimension; i ++) {
if(array[i] == word) {
duplicated = true;
break;
}
}
return duplicated;
}
Upvotes: 0
Reputation: 11146
You asked: select a random word, convert it to chars in an array, but how do i search that array for duplicates?
Using boost, this may be boiled down to:
const bool hasDuplicates = boost::size(letters) != boost::size(boost::unique(letters));
Upvotes: 0
Reputation: 100
You can also find duplicate words using hashing..
1.first create hash table.
2.one by one traverse words.
3.for every word check if it already exists or not..if it is already present print word otherwise insert it into hash.
you can use unordered_set<string> s
for hashing.
void printDup(vector<string> words)
{
unordered_set<string> s;
bool f = false;
for(int i = 1; i<words.size(); i++)
{
if (s.find(words[i]) != s.end())
{
cout << words[i] << endl;
f = true;
}
else
s.insert(words[i]);
}
if(f == false)
cout << "No Duplicate words" << endl;
}
Upvotes: 0
Reputation: 75924
std::adjacent_find
is your friend:
template< class ForwardIt > ForwardIt adjacent_find( ForwardIt first, ForwardIt last );
Searches the range [first, last) for two consecutive identical elements.
Return value
an iterator to the first of the first pair of identical elements [...] If no such elements are found, last is returned
First sort the array, then do adjacent_find
on it and check if it returns last or not.
Upvotes: 3