I want to count all the different string elements in an array. So my input would be:
5 Lemon Orange Lemon Mango Lemon
And the output should be this:
3
The problem with my code is, that my code counts all the elements, and not just the different and I can't figure out why.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int N;
cin >> N;
string Tname;
string data[N];
int counter = 0;
for(int i = 0; i<N; i++)
{
cin >> Tname;
data[i] = Tname;
}
for(int l = 0; l<N; l++)
{
int k = 0;
while(k<N && (data[l] != data[k]))
{
k++;
}
if(k<N)
{
counter += 1;
}
}
cout << counter << endl;
return 0;
}