John Park
John Park

Reputation: 290

Removing and storing duplicates in an array

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.

Upvotes: 0

Views: 1657

Answers (3)

Csaba Toth
Csaba Toth

Reputation: 10729

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, " " ) );
}

Upvotes: 2

Christian Garbin
Christian Garbin

Reputation: 2532

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, " " ) );
}

Upvotes: 3

jmihalicza
jmihalicza

Reputation: 2089

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.

Upvotes: 1

Related Questions