Hi, I have a large array of strings. I want to figure out if there any duplicate entries in this array and if there is add 1 to a variable called count (which keeps track of the total number of duplicated values in the whole array).
Any suggestions on the best way of solving this problem?
The best way? Im pretty sure this is the one that works and that you can understand.
The simple way is to create a loop similar to a bubble sort
1 2 3 4 5 6 7 8 9 10
for (int i = 0; i < arrLength - 1; i++)
{
// cout << myArr[i] << '\n';
for (int j = i + 1;j < arrLength; j++)
{
// cout << '\t' << myArr[j] << '\n';
if (myArr[i] == myArr[j]) // then this is a duplicate
}
}
If someone said this took too long, I would create another data structure, an associative container. I would keep this container unique, so something like this:
1 2 3 4 5 6 7
AContainer c;
int count = 0;
for (int i = 0; i < myArrLength; i++)
{
if (!c.insert(myArr[i])) // this is a duplicate
count++;
}