Counting not equal strings in array in C++










2















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;










share|improve this question



















  • 3





    This string data[N]; is not valid C++.

    – Neil Butterworth
    Nov 10 '18 at 21:36






  • 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 '18 at 21:53
















2















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;










share|improve this question



















  • 3





    This string data[N]; is not valid C++.

    – Neil Butterworth
    Nov 10 '18 at 21:36






  • 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 '18 at 21:53














2












2








2








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;










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '18 at 21:57









Christophe

39k43474




39k43474










asked Nov 10 '18 at 21:36









BenedekBenedek

183




183







  • 3





    This string data[N]; is not valid C++.

    – Neil Butterworth
    Nov 10 '18 at 21:36






  • 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 '18 at 21:53













  • 3





    This string data[N]; is not valid C++.

    – Neil Butterworth
    Nov 10 '18 at 21:36






  • 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 '18 at 21:53








3




3





This string data[N]; is not valid C++.

– Neil Butterworth
Nov 10 '18 at 21:36





This string data[N]; is not valid C++.

– Neil Butterworth
Nov 10 '18 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 '18 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 '18 at 21:53













3 Answers
3






active

oldest

votes


















0














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






share|improve this answer

























  • 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 '18 at 22:00











  • @super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items

    – Christophe
    Nov 10 '18 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 '18 at 22:35











  • @Benedek glad to know that it helped :-) Thanks for this feedback

    – Christophe
    Nov 10 '18 at 22:48


















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;






share|improve this answer























  • 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 '18 at 22:38


















0














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.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    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

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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






    share|improve this answer

























    • 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 '18 at 22:00











    • @super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items

      – Christophe
      Nov 10 '18 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 '18 at 22:35











    • @Benedek glad to know that it helped :-) Thanks for this feedback

      – Christophe
      Nov 10 '18 at 22:48















    0














    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






    share|improve this answer

























    • 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 '18 at 22:00











    • @super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items

      – Christophe
      Nov 10 '18 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 '18 at 22:35











    • @Benedek glad to know that it helped :-) Thanks for this feedback

      – Christophe
      Nov 10 '18 at 22:48













    0












    0








    0







    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






    share|improve this answer















    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 '18 at 22:08

























    answered Nov 10 '18 at 21:53









    ChristopheChristophe

    39k43474




    39k43474












    • 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 '18 at 22:00











    • @super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items

      – Christophe
      Nov 10 '18 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 '18 at 22:35











    • @Benedek glad to know that it helped :-) Thanks for this feedback

      – Christophe
      Nov 10 '18 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 '18 at 22:00











    • @super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items

      – Christophe
      Nov 10 '18 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 '18 at 22:35











    • @Benedek glad to know that it helped :-) Thanks for this feedback

      – Christophe
      Nov 10 '18 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 '18 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 '18 at 22:00













    @super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items

    – Christophe
    Nov 10 '18 at 22:10






    @super oops, indeed. I've edited, keeping the loop structure, but only looking at previous items

    – Christophe
    Nov 10 '18 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 '18 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 '18 at 22:35













    @Benedek glad to know that it helped :-) Thanks for this feedback

    – Christophe
    Nov 10 '18 at 22:48





    @Benedek glad to know that it helped :-) Thanks for this feedback

    – Christophe
    Nov 10 '18 at 22:48













    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;






    share|improve this answer























    • 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 '18 at 22:38















    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;






    share|improve this answer























    • 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 '18 at 22:38













    0












    0








    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;






    share|improve this answer













    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;







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 '18 at 22:00









    Mohamed Ali RACHIDMohamed Ali RACHID

    1,831316




    1,831316












    • 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 '18 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 '18 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 '18 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 '18 at 22:38











    0














    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.






    share|improve this answer



























      0














      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.






      share|improve this answer

























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 '18 at 22:03









        Berecz BalázsBerecz Balázs

        245




        245



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)