Finding and replacing duplicates in string

Pages: 12
Hey guys, I'm trying to make a program which will count the number of unique words in sting then finding the duplicates words and replace them to make them all unique.The first line represent the number of words which will be input
Example
Input. Output
6. 4(number of unique words)
Banana. Banana
Mango. Mango
Apple. Apple
Orange. Orange
Apple. Apple1
Apple. Apple2
The duplicates must be replaced with numbers in front to differentiate them



So far, I have only the program to find duplicates



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

#include<iostream>
#include<map>
#include<string>
#include<vector>

using namespace std;

int main(){
vector <string> sentence;
string input;
while (cin>>input){
    sentence.push_back(input);
}
vector <string> dups;
map <string, unsigned int> counter_of_each_word;
for (int i=0; i<sentence.size(); i++){

    counter_of_each_word[sentence[i]]++;

    if (counter_of_each_word[sentence[i]]==2){
        dups.push_back(sentence[i]);
    }
}
for (int i=0; i<dups.size(); i++){
    cout<<dups[i]<<" ";
}
}



Last edited on
Look into the set container. It will only take unique values. http://www.cplusplus.com/reference/set/set/

I think the emplace function will give you the best result since it's return value will show if the word was already in the set. Please look it up because the return value is actually a pair...

You will still have trouble with punctuation marks and similar, but you can write a dropNonAlpha function pretty easily.

Last edited on
Sir please slow down I'm not that experienced
Could you please tell in more details
First. Indentation helps to see the scopes.

The code below almost, but not quite, does the deed.
The logic is to insert only unique values to the 'sentence'.
However, the way we check for uniqueness and make words unique has a flaw.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>

int main(){
  using std::string; using std::vector; using std::cout;
  const string data = "Banana Mango Apple2 Apple Orange Apple Apple";
  std::istringstream in( data ); 
  vector<string> sentence;
  string input;
  std::map<string, unsigned int> counter_of_each_word;
  while ( in >> input ) {
    auto count = ++counter_of_each_word[input];
    if ( 1 < count ) input += std::to_string( count );
    sentence.emplace_back(input);
  }

  for ( auto w : sentence ) {
    cout << w << " ";
  }
  cout << '\n' << counter_of_each_word.size() << '\n';
}
Thanks let me try to work on it
The first duplicate of apple should be Apple1 not Apple2
Sorry
And it should allow user input
This is almost, but not quite the same as @keskiverto.

Depending on what/how you want to process duplicates you might consider just using the map you started off with and forget about vectors.

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
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
    map<string, int> word_map;
    map<string, int>::iterator iter;
    
    string input;
    while (cout << "Enter a fruit: " && cin >> input && input != "q") // <-- ????
    {
        iter = word_map.find( input );
        if ( iter != word_map.end() )
        {
            iter->second++;
            cout << input << " already encountered " <<  iter->second << " times\n";
        }
        else
            word_map.insert({input,0});
    }
    return 0;
}


The map keeps track of (real) words and how often they are repeated. The rest follows by simply looping through the number of repetitions and adding the increment number to the string - one possibility of many.
Thanks but I think @keskiverto's programm is the one I'm looking for tho I just need to allow the program to accept user input and the first duplicate should have one in front of it and etc
@Dee5

It's all good but please green tick this and your other un-ticked but resolved posts if they are resolved.

It's a time-waster for people here otherwise.
Sorry I forgot but this hasn't been solved I'm just trying to get some help
So what do you want help with?
First allowance of user input when the first line represent amount of things to be entered
Then second line should be the things

Ex input
5
Banana
Orange
Apple
Banana
Orange

Output
4 (count of unique fruits)
Banana
Orange
Apple
Banana2
Orange 2
Sorry unique words should be three
And where’s the problem in reading in an integer and using a for loop to read in the values? Your original code shows you can do that.
I almost forgot about the loop
Thanks
Someone please help
I can't make the program to allow input please and it should be inputted vertically
Here's what I got so far but the "Enter fruits" keep on repeating while I want it to just appear once. Then the output comes out all wrong

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
#include <iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>
using namespace std;
int main(){
  using std::string; using std::vector; using std::cout;
  int N;
  cout << "Enter number of fruits to be entered:";
  cin >> N;
  cout <<"Enter words";
  for(int i=0; i<N; i++)
  {
  string data;
  cin >> data;
  vector<string> sentence;
  string input;
  std::map<string, unsigned int> counter_of_each_word;
  while ( in >> input ) {
    auto count = ++counter_of_each_word[input];
    if ( 1 < count ) input += std::to_string( count );
    sentence.emplace_back(input);
  }}

  for ( auto w : sentence ) {
    cout << w ;
    cout <<'\n';
  }
  cout << '\n' << counter_of_each_word.size() ;
}
Last edited on
1
2
3
4
5
for(int i=0; i<N; i++)
{
    cout <<"Enter words \n";
    cin >> data;
}
You're just overwriting data each time.

I don't see why you need to use a stringstream here.
Just do something like:
1
2
3
4
5
6
7
vector<string> sentence;
cout << "Enter words: ";
for (int i = 0; i < N; i++) {
    string data;
    cin >> data;
    sentence.emplace_back(data);
}
Last edited on
Ive kinda did it but the outcome is totally wrong
The program doesn't seem to limit the user input like the loop doesn't help at all
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
#include <iostream>
#include<sstream>
#include<map>
#include<string>
#include<vector>
using namespace std;
int main(){
  using std::string; using std::vector; using std::cout;
  string data;
  int N;
  cout << "Enter number of fruits to be entered:";
  cin >> N;
  std::istringstream in( data ); 
  vector<string> sentence;
  cout << "Enter words\n";
  
  for(int i=0; i<N ;i++){
  string data;
  cin >> data;
  
  std::map<string, unsigned int> counter_of_each_word;
  while ( in >> data ) {
    auto count = ++counter_of_each_word[data];
    if ( 1 < count ) data += std::to_string( count );
    sentence.emplace_back(data);
  }

  for ( auto w : sentence ) {
    cout << w ;
    cout <<'\n';
  }
  cout << '\n' << counter_of_each_word.size() ;
}
Last edited on
Pages: 12