C Program to Delete Duplicate Elements from an Array

//WAP TO DELETE DUPLICATES FROM THE ARRAY

#include<stdio.h>

int main()
{
	int A[] = {1,3,4,3,5,4,4,4},i,j,k;
	int n = sizeof(A)/sizeof(A[0]);

	printf("Before Deleting Duplicate Elements :--> ");
	for(i=0; i<n; i++)
        printf("%d  ", A[i]);

	for(i = 0; i < n-1; i++)
	{
		for(j = i + 1; j < n; j++)
		{
			if(A[i] == A[j])
			{
				for(k = j; k < n; k++)
				{
					A[k] = A[k+1];  //here left shift is there so that element gets deleted
	;			}
				j--;	//if the position of the element is changed then don't increase the index j
				n--;    //here by the left shift there is one empty element in the end and hence reduce the size of the array
			}
		}
	}

	printf("\nAfter Deleting Duplicate Elements :--> ");
	for(k = 0; k < n; k++)
	{
		printf("%d ", A[k]);
	}


	return 0;
}

<< Previous | Next >>


  • C++ Program to Delete Duplicate Elements from an Array
  • Java Program to Delete Duplicate Elements from an Array
  • Python Program to Delete Duplicate Elements from an Array