Removing duplicates

I believe 2 posts recently have covered this briefly, but I haven't found anything definitive. (1) I need to take a file and output data that includes both duplicates and (2) non-duplicates, then (3) outputs the results in a nice, clean sequence. I am totally confused as to how to look for duplicates, which then complicates step 3 as well. void removeduplicates () is the function I do not know how to even begin on.

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <algorithm>

using namespace std;
void Create_Array();


void Create_Array ()
{
string pirates[5];

string Line[100];

short loop1;

short loop2;



ifstream myfile ("ballots.txt");

if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line[100]);

cout << Line[100] << endl;
int Line [100];

}
}
}

void removeduplicates ()
{



ifstream myfileopen("counted.txt");





}


int main()
{
cout << "This is the ballots with duplicates." <<endl;
Create_Array();
cout << "Here is the ballots without duplicates." <<endl;


return (0);
}
1) Use [code[b][/b]] tags and proper indentation.

2) An array creates N objects, numbered 0 through N-1. Thus: [code]line[100][/code] is invalid.
That said, why do you need 100 strings anyway? Just use one.

    string line;

3) Capitalization matters. "Line" != "line".

4) Don't loop on EOF.

1
2
3
4
while (getline(myfile,line))
{
   cout << line << "\n";
}

5) To remove duplicates, use a std::set. Here's an example with integers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <set>
using namespace std;

int main()
{
  set<int> xs;
  xs.insert( 12 );
  xs.insert( -7 );
  xs.insert( 24 );
  xs.insert( 12 );
  xs.insert( 42 );

  cout << "xs contains " << xs.size() << " elements:";
  for (int x : xs)
    cout << " " << x;
  cout << "\n";
}

6) When you read your file, store the (names?) you get in a std::vector (or, if you must, an array). When you are ready to remove duplicates, you can copy the elements to a set<string> or you can #include <algorithm> and use std::sort() and std::unique():

1
2
  sort( begin(names), end(names) );
  names.erase( unique( begin(names), end(names) ), end(names) );

Hope this helps.
closed account (48T7M4Gy)
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 <string>

using std::cout;
using std::string;
using std::endl;

int main()
{
	string nameList[] = { "Trump","Smith", "Brown","Brown","Jones", "Bloggs", "Brown","Smith","Bloggs","Trump" };
	int size = sizeof(nameList) / sizeof(string);

	string temp = "";
	for (int i = 0; i < size; i++)
	{
		temp = nameList[i];
		for (int j = i + 1; j < size; j++)
		{
			if (temp == nameList[j])
				nameList[j] = "";
		}
	}

	for (int i = 0; i < size; i++)
	{
		cout << nameList[i] << endl;
	}
}


Probably not the best for efficiency but for a few hundred names ... albeit using the dreaded and accursed arrays as well.
Last edited on
Thank you Duoas and kemort!
Topic archived. No new replies allowed.