# Program to remove duplicate elements in an array | faceprep

Program to remove duplicate elements in an array (sorted and unsorted array cases) are discussed here. Given an array, all the duplicate elements of the array are removed.

For example, consider the array

case 1: Remove duplicates from sorted array

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

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

case 2: Remove duplicates from unsorted array

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

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

## Algorithm to remove duplicate elements in an array (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 an array (sorted array ) is given below.

/* C program to remove duplicate elements in an array */
#include<stdio.h>

int remove_duplicate_elements(int arr[], int n)
{

if (n==0 || n==1)
return n;

int temp[n];

int j = 0;
int i;
for (i=0; i<n-1; i++)
if (arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[n-1];

for (i=0; i<j; i++)
arr[i] = temp[i];

return j;
}

int main()
{
int n;
scanf(“%d”,&n);
int arr[n];
int i;
for(i = 0; i < n; i++)
{
scanf(“%d”,&arr[i]);
}

n = remove_duplicate_elements(arr, n);

for (i=0; i<n; i++)
printf(“%d “,arr[i]);

return 0;
}

/* C++ program to remove duplicate elements in an array */
#include<iostream>
using namespace std;

int remove_duplicate_elements(int arr[], int n)
{

if (n==0 || n==1)
return n;

int temp[n];

int j = 0;
int i;
for (i=0; i<n-1; i++)
if (arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[n-1];

for (i=0; i<j; i++)
arr[i] = temp[i];

return j;
}

int main()
{
int n;
cin >> n;
int arr[n];
int i;
for(i = 0; i < n; i++)
{
cin >> arr[i];
}

n = remove_duplicate_elements(arr, n);

for (i=0; i<n; i++)
cout << arr[i] << ” “;

return 0;
}

/* Java program to remove duplicate elements in an array */

import java.util.Scanner;
public class Main
{
public static int remove_duplicate_elements(int arr[], int n)
{

if (n==0 || n==1)
return n;

int temp[] = new int[n];

int j = 0;
int i;
for (i=0; i<n-1; i++)
if (arr[i] != arr[i+1])
temp[j++] = arr[i];
temp[j++] = arr[n-1];

for (i=0; i<j; i++)
arr[i] = temp[i];

return j;
}

public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print(“Enter no. of elements you want in array:”);
n = s.nextInt();
int a[] = new int[n+1];
System.out.println(“Enter all the elements:”);

for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}

n = remove_duplicate_elements(a, n);
System.out.print(“Array after removing : “);
for (int i=0; i<n; i++)
System.out.print(a[i] + ” “);

}
}

# Python program to remove duplicate elements in an array

def remove_duplicate_elements(arr, n):

if n == 0 or n == 1:
return n

temp = list(range(n))

j = 0;
for i in range(0, n-1):

if arr[i] != arr[i+1]:
temp[j] = arr[i]
j += 1

temp[j] = arr[n-1]
j += 1

for i in range(0, j):
arr[i] = temp[i]

return j

n = int(input())
sum = 0
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
n = remove_duplicate_elements(arr,n)
print(“Array after removing : “,end = “”)
for i in range(0,n):
print(arr[i], end = ” “)

#include<stdio.h>
#include<stdlib.h>
int i,j;
int removed(int*,int);
int main()
{
int n,*a;
scanf(“%d”,&n);
a=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
scanf(“%d”,(a+i));

removed(a,n);

return 0;
}
int removed(int*a,int n)
{
int k;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)
{
if(*(a+i)==*(a+j))
{
for(k=j;k<n;k++)
{
*(a+k)==*(a+k+1);
}
n–;
}
else
j++;
}
}
for(i=0;i<n;i++)
printf(“%d\n”,*(a+i));
return 0;
}

## Algorithm to remove duplicate elements in an array (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 array (unsorted array) is given below.

// C++ program to remove the duplicateelements in an array
#include <iostream>
#include <unordered_map>
using namespace std;

void remove_duplicate_elements(int arr[], int n)
{
// Hash map which will store the elements which has appeared previously.
unordered_map<int, bool> mp;

for (int i = 0; i < n; ++i) {

// Print the element if it is there in the hash map
if (mp.find(arr[i]) == mp.end()) {
cout << arr[i] << ” “;
}

// Insert the element in the hash map
mp[arr[i]] = true;
}
}

int main(int argc, char const* argv[])
{
int n;
cin >> n;
int arr[n];
int i;
for(i = 0; i < n; i++)
{
cin >> arr[i];
}
remove_duplicate_elements(arr, n);
return 0;
}

Output: