0

The objective of this program is to remove duplicates from an array

Write a program that inputs an array of 10 integers from the user, and removes the duplicate array elements.

Here is some sample output: Please enter 10 integers, hitting return after each one: 5 75 10 75 5 80 10 5 5 50 You entered 5 unique numbers: 5 75 10 80 50

here is my code so far

#include <iostream>
using namespace std;

int main()
{
int myint[11];
int i,x,count=10;

cout << "Please input 10 integers, hitting return after each one \n";
    for(i=0;i<10;i++){
    cin>> myint[i];
    }

    for(i=0;i<=10;i++)
    {
            for(x=i+1;x<=10;x++)
            {
                    if(myint[x]==myint[i])
                    {
                            count--;
                            for(i=x;i<=count;i++)
                            { myint[i] = myint[i+1];
                            }
                    }
            }

      }
cout << endl;
cout << " You entered "<< count << " unique numbers: " <<  endl;

    for(i=0;i<count;i++){
    cout << myint[i] << " ";
    }
return 0;
}

here is my output Please input 10 integers, hitting return after each one 5 75 10 75 5 80 10 5 5 50

You entered 7 unique numbers: 5 75 10 75 80 10 5

The duplicates have to be removed or written over and the unique numbers should be placed into a new array and not just displayed on the screen. Im not entirely sure where my error is. It seems somewhere the first time the loop runs it seems to be finding a duplicate no matter what and throwing the rest of the loops in the array off? Im kind of lost. Any help at all is appreciated. Thanks.

John Park
  • 290
  • 2
  • 8
  • 25
  • You should be able to debug this by stepping through line by line in the debugger and watching what happens to your variables. – Jason May 07 '13 at 22:45

3 Answers3

3

Since the question is tagged as C++, you may as well make use of C++ idioms in the code. Let sort and unique do the heavy lifting.

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, const char * argv[])
{
   vector<int> v;

   cout << "Please input 10 integers, hitting return after each one \n";
   for( int i = 0; i < 10; i++ ) {
      int num;
      cin >> num;
      v.push_back(num);
   }

   sort( v.begin(), v.end() );
   v.erase( unique( v.begin(), v.end() ), v.end() );

   cout << endl << " You entered " << v.size() << " unique numbers: " <<  endl;
   copy( v.begin(), v.end(), ostream_iterator<int>( cout, " " ) );
}
Christian Garbin
  • 2,512
  • 1
  • 23
  • 31
  • Even nicer solution can be to use std:set. That would take care of duplicates by it's nature. Sorting is not a criteria according to the opening note. – Csaba Toth May 07 '13 at 23:46
  • `sort` is needed to make `unique` work for this case. `set` requires more code and more memory (to hold the copies of the elements), although it can be more efficient for large amounts of data, as nicely discussed in http://stackoverflow.com/questions/1041620/most-efficient-way-to-erase-duplicates-and-sort-a-c-vector – Christian Garbin May 08 '13 at 00:03
2

Here is the solution with set I mentioned in @chr's solution:

#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>

using namespace std;

int main(int argc, const char* argv[])
{
   set<int> s;

   cout << "Please input 10 integers, hitting return after each one \n";
   for( int i = 0; i < 10; i++ ) {
      int num;
      cin >> num;
      s.insert(num);
   }
   cout << endl << " You entered " << s.size() << " unique numbers: " <<  endl;
   copy( s.begin(), s.end(), ostream_iterator<int>( cout, " " ) );
}
Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
1

The innermost cycle reuses i, which is the variable of the outermost cycle. Use another letter for that.

It is also strange that if you want to read 10 elements, why you have an array and corresponding loops of 11. Moreover, you can limit your loops with < count instead of <= 10.

jmihalicza
  • 2,083
  • 12
  • 20