How to find duplicates from an Array of Strings

Mar 12, 2013 at 2:08pm
closed account (oj8RX9L8)
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?
Last edited on Mar 12, 2013 at 2:08pm
Mar 12, 2013 at 2:25pm
Im not sure if this is what you mean.

1
2
3
4
5
6
7
8
9
10
11
string mystring[50];
string aux;
int counter(0);

for (int x=0;x<=50;x++)
{
   aux=mystring[x]
   if (aux==mystring[x])
      counter=counter+1;

 }
Mar 12, 2013 at 2:27pm
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++;
}
Last edited on Mar 12, 2013 at 2:28pm
Mar 12, 2013 at 2:34pm
for(int i = 0;i < 20; i++) {
for(int j = i; j < 20; j++)
{
if(j != i)
{
if(myString[i] ==mystring[j])
{
count++;
}
}


Im a beginner! DOnt shout if its wrong haha
Topic archived. No new replies allowed.