Andrew Glass
Andrew Glass

Reputation: 415

c++ remove duplicate numbers from a file

im trying to make a program in c++ which would work through a txt file, and if there are duplicates in the numbers in this file, dont print them and only print out the numbers which appear once.

this is the code i've got. but what happens is it prints the file out, then prints out the second line again instead of looking for dublicates...

can anyone show me where im going wrong. fairly new to c++

// array.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
    int array[100]; // creates array to hold numbers
    short loop=0; //short for loop for input
    string line; //this will contain the data read from the file
    ifstream myfile ("problem3.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
        while (! myfile.eof() ) //while the end of file is NOT reached
        {
            getline (myfile,line); //get one line from the file
            array[loop] = line;
            cout << array[loop] << endl; //and output it
            loop++;
        }

        for (int i = 1; i < loop; i++)
        {
            bool matching = false;
            for (int j = 0; (j < i)&& (matching == false); j++)
            {
                 if (array[i] == array[j]) 
                      matching = true;
             }
             if (!matching) 
                cout<< array[i] << " "
        }   
        myfile.close(); //closing the file
     }
      else 
         cout << "Unable to open file"; //if the file is not open output
     system("PAUSE");
     return 0;
 }

Upvotes: 0

Views: 5289

Answers (2)

taocp
taocp

Reputation: 23654

At least one error: array is declared as an array of integers, you are reading string line and assign string to int directly below:

 getline (myfile,line); //^^line is string, array[i] is int
 array[loop] = line;

You may try to read those lines in a vector then call std::unique to make the vector unique and print them out. You file lines are not necessarily lines of integers, so store them in an integer array may not work.

You may try:

#include <vector>
#include <string>
#include <algorithm>
#include <iterator> 

int main()
{
    std::vector<std::string> data;
    ifstream myfile ("problem3.txt");
    string line;
    //if not required to use array
    while (getline(myfile, line))
    {
      data.push_back(line);
    }

    std::vector<std::string> data(dataArray, dataArray + 100);

    myfile.close();
    std::sort( data.begin(), data.end() );
    data.erase( std::unique( data.begin(), data.end()), data.end() );
    //now print vector out:
    std::copy(data.begin(), data.end(), ostream_iterator<string>(cout, " \n"));
    return 0;
}

Upvotes: 1

Victor Sand
Victor Sand

Reputation: 2340

Instead of using nested for loops, I would suggest using an array to keep count of how many times a certain integer shows up. You can then loop over that once and only print the integers with a count of 1.

One of the problems with your nested for loops is since you're only checking for duplicates as long as j < i, you might print an integer even though it has a duplicate later in the array. You simply have to check the whole array to make sure that there are no duplicates.

If you still want to try, you probably want to do something like this:

for (int i = 0; i < loop; i++) // start at 0!
{
   bool matching = false;
   for (int j=0; j<loop; j++) { // check the whole array for duplicates!
       if (i != j && array[i] == array[j]) {
           matching = true;
           break; // instead of complicated condition in for loop
       }
   }
   if (!matching) cout << array[i] << " ";
}  

Upvotes: 0

Related Questions