Counting not equal strings in array in C++
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;
c++ arrays string count
add a comment |Â
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;
c++ arrays string count
3
Thisstring data[N];
is not valid C++.
â Neil Butterworth
Nov 10 at 21:36
1
I suppose just loading astd::unordered_set<std::string>
and reporting thesize()
upon loop completion is out of bounds. Like this.
â WhozCraig
Nov 10 at 21:53
add a comment |Â
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;
c++ arrays string count
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;
c++ arrays string count
c++ arrays string count
edited Nov 10 at 21:57
Christophe
38.9k43474
38.9k43474
asked Nov 10 at 21:36
Benedek
183
183
3
Thisstring data[N];
is not valid C++.
â Neil Butterworth
Nov 10 at 21:36
1
I suppose just loading astd::unordered_set<std::string>
and reporting thesize()
upon loop completion is out of bounds. Like this.
â WhozCraig
Nov 10 at 21:53
add a comment |Â
3
Thisstring data[N];
is not valid C++.
â Neil Butterworth
Nov 10 at 21:36
1
I suppose just loading astd::unordered_set<std::string>
and reporting thesize()
upon loop completion is out of bounds. Like this.
â WhozCraig
Nov 10 at 21:53
3
3
This
string data[N];
is not valid C++.â Neil Butterworth
Nov 10 at 21:36
This
string data[N];
is not valid C++.â Neil Butterworth
Nov 10 at 21:36
1
1
I suppose just loading a
std::unordered_set<std::string>
and reporting the size()
upon loop completion is out of bounds. Like this.â WhozCraig
Nov 10 at 21:53
I suppose just loading a
std::unordered_set<std::string>
and reporting the size()
upon loop completion is out of bounds. Like this.â WhozCraig
Nov 10 at 21:53
add a comment |Â
3 Answers
3
active
oldest
votes
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
â super
Nov 10 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
â Christophe
Nov 10 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
â Benedek
Nov 10 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
â Christophe
Nov 10 at 22:48
add a comment |Â
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#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;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
â Benedek
Nov 10 at 22:38
add a comment |Â
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
â super
Nov 10 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
â Christophe
Nov 10 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
â Benedek
Nov 10 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
â Christophe
Nov 10 at 22:48
add a comment |Â
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
â super
Nov 10 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
â Christophe
Nov 10 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
â Benedek
Nov 10 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
â Christophe
Nov 10 at 22:48
add a comment |Â
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
The problem is algorithmic: every item is equal to itself, which will end your k loop prematurely. In addition, you only increment when the item is repeated.
I propose you to change the loops, so not to compare every items with every other items, but only items, to items previously processed:
for(int l = 0; l<N; l++)
int k = 0;
while(k<l && data[l] != data[k]) // only previous items
k++;
if(k==l) // if no identical, we can add this one
cout<<l<<" "<<data[l]<<endl;
counter += 1;
Not related: variable length arrays are not legal C++ even if some mainstream compilers accept it. I'd suggest to use a vector to emulate this feature: vector<string> data(N);
Online demo
edited Nov 10 at 22:08
answered Nov 10 at 21:53
Christophe
38.9k43474
38.9k43474
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
â super
Nov 10 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
â Christophe
Nov 10 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
â Benedek
Nov 10 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
â Christophe
Nov 10 at 22:48
add a comment |Â
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
â super
Nov 10 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
â Christophe
Nov 10 at 22:10
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
â Benedek
Nov 10 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
â Christophe
Nov 10 at 22:48
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
â super
Nov 10 at 22:00
This doesn't fix the logic. This is counting 3 lemons and it just happens to be the same as the answer.
â super
Nov 10 at 22:00
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
â Christophe
Nov 10 at 22:10
@super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items
â Christophe
Nov 10 at 22:10
1
1
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
â Benedek
Nov 10 at 22:35
Yeah, thank you, this resolved my code, and now I understand what was the problem in my logic. Thanks a lot.
â Benedek
Nov 10 at 22:35
@Benedek glad to know that it helped :-) Thanks for this feedback
â Christophe
Nov 10 at 22:48
@Benedek glad to know that it helped :-) Thanks for this feedback
â Christophe
Nov 10 at 22:48
add a comment |Â
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#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;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
â Benedek
Nov 10 at 22:38
add a comment |Â
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#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;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
â Benedek
Nov 10 at 22:38
add a comment |Â
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#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;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
if i understand well your problem , you want the value that has the max appearances in the array, some modifications are needed to achieve this :
#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;
int tempCounter; // a temporary counter for each item of the array .
for(int l = 0; l < N; l++)
tempCounter = 0;
int k = 0;
while(k<N)
if(data[l] == data[k])
tempCounter++;
k++;
if(tempCounter > counter) // if the new counter is higher than the counter
counter = tempCounter;
cout << counter << endl;
return 0;
answered Nov 10 at 22:00
Mohamed Ali RACHID
1,801315
1,801315
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
â Benedek
Nov 10 at 22:38
add a comment |Â
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
â Benedek
Nov 10 at 22:38
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
â Benedek
Nov 10 at 22:38
Not the max appearances. I wanted to count all the different elements in the array. Sorry if my explanation wasn't accurate. But thanks for the help, I think i learn from that as well.
â Benedek
Nov 10 at 22:38
add a comment |Â
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
add a comment |Â
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
add a comment |Â
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
the last if should be if(k+1==N)
because you all the time stop the while before the k reach N
and k must start from l
Your logic is, you add 1 to the counter if it is not in the list's remaining part
But the code check the full list so you never count thw world whitch in the list twice.
answered Nov 10 at 22:03
Berecz Balázs
245
245
add a comment |Â
add a comment |Â
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243639%2fcounting-not-equal-strings-in-array-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
This
string data[N];
is not valid C++.â Neil Butterworth
Nov 10 at 21:36
1
I suppose just loading a
std::unordered_set<std::string>
and reporting thesize()
upon loop completion is out of bounds. Like this.â WhozCraig
Nov 10 at 21:53