Program to remove duplicate elements in an array | faceprep

Program to remove duplicate elements in an array | faceprep

This article talks about the program that eliminates duplicate elements from an array. Duplicate elements are those that appear more than once in a given array, they can be observed in both sorted and unsorted arrays. In order to create an array that only contains unique elements, we need to remove any duplicate elements.

program to remove duplicate elements in an array poster


Case 1: Remove duplicates from the sorted array

Input: arr = {1, 2, 3, 4, 4}

Output: arr = {1, 2, 3, 4}

Case 2: Remove duplicates from the unsorted array

Input: arr = {9, 2, 7, 4, 7}

Output: arr = {9, 2, 7, 4}

Algorithm to remove duplicate elements in a sorted array

  • Input the number of elements of the array.
  • Input the array elements.
  • Repeat from i = 1 to n
  • – if (arr[i] != arr[i+1])
  • – temp[j++] = arr[i]
  • – temp[j++] = arr[n-1]
  • Repeat from i = 1 to j
  • – arr[i] = temp[i]
  • return j.


program to remove duplicate elements in a sorted arrayClick here to learn more about FACE Prep PRO


Program to remove duplicate elements in a sorted array

@@coding::1@@

Program to remove duplicate elements in a sorted array using pointers

The following is the program used to remove duplicate elements in a sorted array using pointers

//Code using pointers n#include<stdio.h>n#include<stdlib.h>nint i,j;nint removed(int*,int);nint main()n{nint n,*a;nscanf(“%d”,&n);na=(int*)malloc(n*sizeof(int));nfor(i=0;i<n;i++)nscanf(“%d”,(a+i));nremoved(a,n);nreturn 0;n}nint removed(int*a,int n)n{nint k;nfor(i=0;i<n;i++)n{nfor(j=i+1;j<n;)n{nif(*(a+i)==*(a+j))n{nfor(k=j;k<n;k++)n{n*(a+k)==*(a+k+1);n}nn–;n}nelsenj++;n}n}nfor(i=0;i<n;i++)nprintf(“%dn”,*(a+i));nreturn 0;n}n

Algorithm to remove duplicate elements in an unsorted array

  • Input the number of elements of the array.
  • Input the array elements.
  • Create a hashmap and store all the elements and their count.
  • Print all the elements from the hashmap having count = 1.


program to remove duplicate elements in an arrayClick here to learn more about FACE Prep PRO


Program to remove duplicate elements in an unsorted array

@@coding::2@@


Suggested Articles



Program to remove duplicate elements in an array

c