Delete Duplicate Elements in Array in C++

Google Advertisements

Prev Tutorial Next Tutorial

Delete Duplicate Elements from Array Program in C++

Array is collection of similar data type, In this code we enter only Integer type values and remove all duplicate value form array list. To delete all duplicate elements from array list you need looping and if else statement.

In below program we compared each element with all other elements. If the element is equal to any other element in the array then shift all the elements to left by one place. The process is repeated till all the elements are compared.

Example

Given Array with Duplicate Elements: 4 5 8 9 5 12 7
New Array with Unique Elements: 4 5 8 9 12 7

C++ Program to Remove Duplicate Elements in an Array

#include<iostream.h>
#include<conio.h>

void main()
{
 int arr[20], i, j, k, size;
 clrscr();
   cout<<"\nEnter Size of an Array: ";
   cin>>size;
 
   cout<<"\nEnter any" <<size <<" Numbers : ";
   for (i = 0; i < size; i++)
      cin>>arr[i];
 
   cout<<"\nArray with Unique list  : ";
   for (i = 0; i < size; i++) 
   {
      for (j = i + 1; j < size;)
	  {
         if (arr[j] == arr[i])
		 {
            for (k = j; k < size; k++)
			{
               arr[k] = arr[k + 1];
            }
            size--;
         }
		 else
            j++;
      }
   }
 
   for (i = 0; i < size; i++) 
   {
      cout<<arr[i];
   }
getch();
}

Output

Enter Size of an Array : 5
Enter any 5 Numbers : 3 4 7 6 4
Array with Unique list  : 3 4 7 6 
delete duplicate elements from array in c++
Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs