Write a C++ program to delete duplicate elements from an array?
Write a program in C++ to delete duplicate elements from an array. How to remove duplicate elements from an array in C++. Logic to delete duplicate values from an array.
Logic
Input with Duplicate elements existed.
Input array elements: 60, 300, 26, 1, 300, 10, 2, 10, 5.
Output with Duplicate elements are removed
Elements of array are: 60, 300, 26, 1, 10, 2, 5.
Program
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 |
#include<iostream> using namespace std; int main() { int s; int arr[s]; int x,y,z; cout<<"enter size in an array"; cin>>s; cout<<"enter elements in array:"; for(x=0;x<s;x++) { cin>>arr[x]; } for(x=0;x<s;x++) { for(y=x+1;y<s;y++) { if(arr[x]==arr[y]) { for(z=y;z<s;z++) { arr[z]=arr[z+1]; } s--; y--; } } } cout<<"array elements after deleting duplicates:"; for(x=0;x<s;x++) { cout<<arr[x]; } } |
Output