Site icon T4Tutorials.com

C++ program to delete duplicate elements from array

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

#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

C++ program to delete duplicate elements from arrayYouTube video player

Exit mobile version