write a c++ program to remove the duplicate occurrence of the value present in an integer array... for example... if the array contains 1,2,3,4,5,1,2,3,4,5... after removing duplicate values... we should get 1,2,3,4,5...
Answers
Answered by
3
This is a simple algorithm. Not efficient one in computations.
You can also use a quicksort program on the array. Then eliminate duplicates. That will be a simpler program.
======================
#include <iostream>
using namespace std;
int main() {
int i, j, k, N; int data[100], n ;
cout << "array size: " ; cin >> N;
cout << "Input data: " ; for (i=0; i <N ; i++) cin >> data[i] ;
n=N;
for (i=1; i<N ; i++) // check if its duplicate
for (j=0 ; j<i ; j++)
if (data[i] == data[j]) {
for(k=i; k<N-1; k++) data[k] = data[k+1];
n--; N--; i--;
break;
}
cout << "\n"<< n << " unique elements: ";
for (i=0; i<n; i++) cout << data[i] << ", " ;
return 0;
}
You can also use a quicksort program on the array. Then eliminate duplicates. That will be a simpler program.
======================
#include <iostream>
using namespace std;
int main() {
int i, j, k, N; int data[100], n ;
cout << "array size: " ; cin >> N;
cout << "Input data: " ; for (i=0; i <N ; i++) cin >> data[i] ;
n=N;
for (i=1; i<N ; i++) // check if its duplicate
for (j=0 ; j<i ; j++)
if (data[i] == data[j]) {
for(k=i; k<N-1; k++) data[k] = data[k+1];
n--; N--; i--;
break;
}
cout << "\n"<< n << " unique elements: ";
for (i=0; i<n; i++) cout << data[i] << ", " ;
return 0;
}
kvnmurty:
thanks for select ing best ans
Similar questions