hi, i m writing code to eliminate the repeating in array.
example :
array[10]= {1,2,3,4,5,6,7,3,8,9}
output :
1 2 3 8 9 // repeatation between 3 is eliminate
array[10]= {1,2,3,4,5,6,4,7,2,9}
output is :
1 2 9 // the repeatation between 4 is eliminate
However, i face the problem in this example:
array[10]= {1,2,3,4,5,3,7,6,7,9}
output is :
1 2 3 7 6 7 9 //the repeatation between 3 is eliminate, but in between 7 is not.
Thank you very much
the code:
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
using namespace std;
main(){
int numb[100];
int numb_temp[100];
int i, j, loop, c, n;
cout<<"Enter number of elements in array\n"<<endl;
cin>>n;
cout<<"Enter "<< n <<" elements\n"<<endl;
for ( c = 0 ; c < n ; c++ ){
cin>>numb[c];}
for(i=0; i<n; i++)
{
numb_temp[i] = numb[i];
for(j=i; j<n; j++)
{
if(numb_temp[i] == numb[j+1])
{
loop = n - ((j+1) - i);
for(int k=j+1; k<=(n-1); k++)
{ numb[i++]=numb[k]; }
break;
}
else continue;
}
}
cout<<"The Result: "<<endl;
for(i=0; i<loop; i++)
{
cout<<numb[i]<<endl;
}
system("pause");
return 0;
}
|