Count duplicates

So im trying to go thru a list of words and count how many times the word was used, it works for the first word, then things go wrong. input and output can be seen below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <vector>

using namespace std;
vector<int> wordCnt;
vector<string> inStr;

int main(){
string strbuff;
while(cin >> strbuff && strbuff != "0"){ //write string, create strcnt vector
    inStr.push_back(strbuff);
    wordCnt.push_back(0);}


auto y = wordCnt.begin();//wordCnt[0] == inStr[0]
bool isChkd = false;

for(auto i = inStr.begin(); i != inStr.end();++i){//loop thru each word

    if(i != inStr.begin()){ //check if index 0
    for(auto z = i-1;z != inStr.begin();--z){ //loop thru words previous to see if checked
        if(*z == *i)
        isChkd = true;}}

    switch(isChkd){
        case true:{ //if true word was checked, set isChkd back to false in case new word
        cout << "Word was checked previously: " << *i << endl;
        isChkd = false;
        break;}

    case false:{ //false for 1st item always
    *y = 1;
    cout << "Finding Dups for: " << *i << endl;
    for(auto x = (i + 1); x != inStr.end();++x){
            if(*x == *i){
            ++*y;
            cout << "MATCH FOUND FOR: " << *i << " COUNT: " << *y << endl;}}
            ++y;
            break;}}}

}

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13
test test test now now 0
Finding Dups for: test
MATCH FOUND FOR: test COUNT: 2
MATCH FOUND FOR: test COUNT: 3
Finding Dups for: test
MATCH FOUND FOR: test COUNT: 2
Word was checked previously: test
Finding Dups for: now
MATCH FOUND FOR: now COUNT: 2
Word was checked previously: now

Process returned 0 (0x0)   execution time : 12.857 s
Press any key to continue.
Last edited on
Use std::map<std::string,int>, if permitted. Loop the list once. If a word is not in map yet, add it and set count to 1. If it is there already, then increase count by one.
I have a better solution for you.
Use maps
The key of a map is unique.
so you use a std::map<string, int>;
You loop through the words and assign the words to the string value, and the int to 0;
then you loop through map and check count how many times each word apears in the line you have and increment the int that is paird with the number.
This solves 2 problems
1. You don't have to make sure you allready checked the word before
2. You can keep a better check on each word and how many times you found the word.
Thanks for your help guys, I haven't gotten to map in the standard library, im following the c++ primer and they wanted me to find duplicates in a lame way, only finding a duplicate if its directly next to it. I wanted to take what I have already learned and make it better by finding the duplicates no matter there order, this morning I did it! My problem was when I checked if the word had been previously tested, I looped till z== inStr.begin() which wouldnt check the first word so I subtracted 1 to loop till one memory address before begin.

1
2
3
4
if(i != inStr.begin()){ //check if index 0
    for(auto z = i-1;z !=( inStr.begin() -1);--z){ //loop thru words previous to see if checked
        if(*z == *i)
        isChkd = true;}}



Topic archived. No new replies allowed.