Remove duplicates from an array

Hi, I got a problem with removing duplicate ints from an array.
Problem is that user will type ints as many as he or she can.
The program need to print out unsorted list of numbers after removing duplicates.

eg. user type: 2 4 2 8 4 8

Then need to print out like this: 2 4 8

This my code but it doesn't work correctly it either removes last integers or adds random values.
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
#include <iostream>
using namespace std;
const int SIZE=20;
int main ()
{
        int numbs[SIZE], value, idx,n;
        cout<<"PLease enter size of an array"<<endl;
        cin>>n;
        cout << "Please enter in a series of numbers "<<endl;
        for(idx = 0; idx < n; idx++)
           cin >>numbs[idx];


       for (idx = 0; idx < n; idx++)
       {
           for(value=n;value>idx;value--)
             if (numbs[idx]==numbs[value])
               {
                  numbs[value]=numbs[value+1];
                  n=n-1;
               }
       }

       for (idx = 0; idx <=n; idx++)
         {
            cout << numbs[idx]<< " " ;
         }
         system("pause");
         return 0;
}



Hello!

From 14 line:

1
2
3
4
5
6
7
cout<< numbs[0] << " ";
for (int i = 1; i < n; i++)
{
	bool matching = false;
	for (int j = 0; (j < i) && (matching == false); j++)if (numbs[i] == numbs[j]) matching = true;
	if (!matching) cout<< numbs[i] << " ";
}	


The first number is printed out because it doesn't act before.
In the first loop you should check all number from 1 till n:

for (int i = 1; i < n; i++)

Variable matching is used for storing the matching under checking process.
In the next loop you should check that the checked number acted before. If it is true than it will be noted in variable matching.

Finally your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
const int SIZE=20;
int main ()
{
        int numbs[SIZE], value, idx,n;
        cout<<"PLease enter size of an array"<<endl;
        cin>>n;
        cout << "Please enter in a series of numbers "<<endl;
        for(idx = 0; idx < n; idx++)
           cin >>numbs[idx];

		
        cout<< numbs[0] << " ";
	for (int i = 1; i < n; i++)
	{
		bool matching = false;
		for (int j = 0; (j < i) && (matching == false); j++)if (numbs[i] == numbs[j]) matching = true;
		if (!matching) cout<< numbs[i] << " ";
	}	
		   
		   
}
Topic archived. No new replies allowed.