An array is a collection of elements with the same data types. It's a linear data structure and is stored in contiguous memory locations. You can perform many basic operations on an array such as insertion, deletion, search, update, traversal, etc.

In this article, you'll learn how to remove duplicate elements from sorted and unsorted arrays.

How to Remove Duplicate Elements From an Unsorted Array

Problem Statement

You're given an unsorted array of integers. You need to remove the duplicate elements from the array and print the array with unique elements.

Example 1: Let arr = [23, 35, 23, 56, 67, 35, 35, 54, 76]

Array after removing duplicate elements: 23 35 56 67 54 76

Thus, the output is 23 35 56 67 54 76.

Example 2: Let arr = [5, 6, 1, 1, 7, 5, 8, 2, 7, 8]

Array after removing duplicate elements: 5 6 1 7 8 2

Thus, the output is 5 6 1 7 8 2.

Approach to Remove Duplicate Elements From an Unsorted Array

You can remove duplicate elements from an unsorted array by following the approach below:

  1. Initialize a hash map that'll store all the unique elements of the array.
  2. Traverse the array.
  3. Check if the element is present in the array.
  4. If the element is present in the array, keep traversing.
  5. If the element is not present in the array, print the element and store it in the hash map.

Note: The time complexity of this solution is O(n).

C++ Program to Remove Duplicate Elements From an Unsorted Array

Below is the C++ program to remove duplicate elements from an unsorted array:

        // C++ program to remove duplicate elements from an unsorted array
#include <bits/stdc++.h>
using namespace std;

// Function to remove duplicate elements from an unsorted array
void removeDuplicateElements(int arr[], int size)
{
 unordered_map<int, bool> m;

for(int i=0; i<size; i++)
 {
 // Print the element if it's not
 // present in the hash map
 if (m.find(arr[i]) == m.end())
 {
 cout << arr[i] << " ";
 }
 // Insert the element in the hash map
 m[arr[i]] = true;
 }

cout << endl;
}

void printArrayElements(int arr[], int size)
{
 for(int i=0; i<size; i++)
 {
 cout << arr[i] << " ";
 }
 cout << endl;
}


int main()
{
 int arr1[] = {23, 35, 23, 56, 67, 35, 35, 54, 76};
 int size1 = sizeof(arr1)/sizeof(arr1[0]);
 cout << "Initial Array: " << endl;
 printArrayElements(arr1, size1);
 cout << "Array after removing duplicates: " << endl;
 removeDuplicateElements(arr1, size1);

 int arr2[] = {5, 6, 1, 1, 7, 5, 8, 2, 7, 8};
 int size2 = sizeof(arr2)/sizeof(arr2[0]);
 cout << "Initial Array: " << endl;
 printArrayElements(arr2, size2);
 cout << "Array after removing duplicates: " << endl;
 removeDuplicateElements(arr2, size2);

 int arr3[] = {32, 35, 33, 32, 33, 38, 32, 39};
 int size3 = sizeof(arr3)/sizeof(arr3[0]);
 cout << "Initial Array: " << endl;
 printArrayElements(arr3, size3);
 cout << "Array after removing duplicates: " << endl;
 removeDuplicateElements(arr3, size3);

return 0;
}

Output:

        Initial Array: 
23 35 23 56 67 35 35 54 76
Array after removing duplicates:
23 35 56 67 54 76
Initial Array:
5 6 1 1 7 5 8 2 7 8
Array after removing duplicates:
5 6 1 7 8 2
Initial Array:
32 35 33 32 33 38 32 39
Array after removing duplicates:
32 35 33 38 39

Related: How to Print "Hello, World!" in the Most Popular Programming Languages

Python Program to Remove Duplicate Elements From an Unsorted Array

Below is the Python program to remove duplicate elements from an unsorted array:

        # Python program to remove duplicate elements from an unsorted list

def removeDuplicateElements(arr, size):
    m = {}

    for i in range(size):
        # Print the element if it's not
        # present in the dictionary
        if arr[i] not in m:
           print(arr[i], end = " ")
        # Insert the element in the dictionary
        m[arr[i]] = 1

    print()


def printListElements(arr, size):
    for i in range(size):
        print(arr[i], end=" ")
    print()

arr1 = [23, 35, 23, 56, 67, 35, 35, 54, 76]
size1 = len(arr1)
print("Initial List: ")
printListElements(arr1, size1)
print("List after removing duplicates: ")
removeDuplicateElements(arr1, size1)

arr2 = [5, 6, 1, 1, 7, 5, 8, 2, 7, 8]
size2 = len(arr2)
print("Initial List: ")
printListElements(arr2, size2)
print("List after removing duplicates: ")
removeDuplicateElements(arr2, size2)

arr3 = [32, 35, 33, 32, 33, 38, 32, 39]
size3 = len(arr3)
print("Initial List: ")
printListElements(arr3, size3)
print("List after removing duplicates: ")
removeDuplicateElements(arr3, size3)

Output:

        Initial Array: 
23 35 23 56 67 35 35 54 76
Array after removing duplicates:
23 35 56 67 54 76
Initial Array:
5 6 1 1 7 5 8 2 7 8
Array after removing duplicates:
5 6 1 7 8 2
Initial Array:
32 35 33 32 33 38 32 39
Array after removing duplicates:
32 35 33 38 39

JavaScript Program to Remove Duplicate Elements From an Unsorted Array

Below is the JavaScript program to remove duplicate elements from an unsorted array:

        // JavaScript program to remove duplicate elements from an unsorted array

// Function to remove duplicate elements from an unsorted array
function removeDuplicateElements(arr, size) {
 let m = new Map();

for (let i = 0; i < size; i++) {
 // Print the element if it's not
 // present in the hash map
 if (m.get(arr[i]) == null) {
 document.write(arr[i] + " ");
 }
 // Insert the element in the hash map
 m.set(arr[i], true);
 }
 document.write("
");
}

function printArrayElements(arr, size) {
 for(let i=0; i<size; i++) {
 document.write(arr[i] + " ");
 }
 document.write("
");
}

let arr1 = [23, 35, 23, 56, 67, 35, 35, 54, 76];
let size1 = arr1.length;
document.write("Initial Array: " + "
");
printArrayElements(arr1, size1);
document.write("Array after removing duplicates: " + "
");
removeDuplicateElements(arr1, size1);

let arr2 = [5, 6, 1, 1, 7, 5, 8, 2, 7, 8];
let size2 = arr2.length;
document.write("Initial Array: " + "
");
printArrayElements(arr2, size2);
document.write("Array after removing duplicates: " + "
");
removeDuplicateElements(arr2, size2);

let arr3 = [32, 35, 33, 32, 33, 38, 32, 39];
let size3 = arr3.length;
document.write("Initial Array: " + "
");
printArrayElements(arr3, size3);
document.write("Array after removing duplicates: " + "
");
removeDuplicateElements(arr3, size3);

Output:

        Initial Array: 
23 35 23 56 67 35 35 54 76
Array after removing duplicates:
23 35 56 67 54 76
Initial Array:
5 6 1 1 7 5 8 2 7 8
Array after removing duplicates:
5 6 1 7 8 2
Initial Array:
32 35 33 32 33 38 32 39
Array after removing duplicates:
32 35 33 38 39

Related: How to Count the Occurrences of a Given Character in a String

How to Remove Duplicate Elements From a Sorted Array

Problem Statement

You're given a sorted array of integers. You need to remove the duplicate elements from the array and print the array with unique elements.

Example 1: Let arr = [1, 1, 1, 2, 4, 6, 8, 8, 9, 9]

Array after removing duplicate elements: 1 2 4 6 8 9

Thus, the output is 1 2 4 6 8 9.

Example 2: Let arr = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]

Array after removing duplicate elements: 1 2 3 4 5

Thus, the output is 1 2 3 4 5.

Approach to Remove Duplicate Elements From a Sorted Array

You can remove duplicate elements from a sorted array by following the approach below:

  1. Initialize the index variables i and j with 0.
  2. Iterate the array.
  3. If the ith element is not equal to the (i+1)th element, then store the ith value in arr[j] and increment the value of j.
  4. Increment the value of i in each iteration.
  5. Store the last value of arr in arr[j].
  6. Finally return the new size of the array i.e., j. Unique elements will be stored in the array from index 0 to j-1.

Note: The time complexity of this solution is O(n).

C++ Program to Remove Duplicate Elements From a Sorted Array

Below is the C++ program to remove duplicate elements from a sorted array:

        // C++ program to remove duplicate elements from a sorted array
#include <iostream>
using namespace std;

// Function to remove duplicate elements from a sorted array
int removeDuplicateElements(int arr[], int size)
{
 int j = 0;

for (int i = 0; i < size-1; i++)
 {
 // If ith element is not equal to (i+1)th element,
 // then store ith value in arr[j]
 if (arr[i] != arr[i+1])
 {
 arr[j] = arr[i];
 j++;
 }
 }

// Storing the last value of arr in arr[j]
 arr[j++] = arr[size-1];

return j;
}

void printArrayElements(int arr[], int size)
{
 for(int i=0; i<size; i++)
 {
 cout << arr[i] << " ";
 }
 cout << endl;
}

int main()
{
 int arr1[] = {1, 1, 1, 2, 4, 6, 8, 8, 9, 9};
 int size1 = sizeof(arr1)/sizeof(arr1[0]);
 cout << "Initial Array: " << endl;
 printArrayElements(arr1, size1);
 cout << "Array after removing duplicates: " << endl;
 size1 = removeDuplicateElements(arr1, size1);
 printArrayElements(arr1, size1);


 int arr2[] = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5};
 int size2 = sizeof(arr2)/sizeof(arr2[0]);
 cout << "Initial Array: " << endl;
 printArrayElements(arr2, size2);
 cout << "Array after removing duplicates: " << endl;
 size2 = removeDuplicateElements(arr2, size2);
 printArrayElements(arr2, size2);

 int arr3[] = {10, 12, 12, 14, 16, 16, 18, 19, 19};
 int size3 = sizeof(arr3)/sizeof(arr3[0]);
 cout << "Initial Array: " << endl;
 printArrayElements(arr3, size3);
 cout << "Array after removing duplicates: " << endl;
 size3 = removeDuplicateElements(arr3, size3);
 printArrayElements(arr3, size3);

return 0;
}

Output:

        Initial Array: 
1 1 1 2 4 6 8 8 9 9
Array after removing duplicates:
1 2 4 6 8 9
Initial Array:
1 1 2 2 3 3 4 4 5 5
Array after removing duplicates:
1 2 3 4 5
Initial Array:
10 12 12 14 16 16 18 19 19
Array after removing duplicates:
10 12 14 16 18 19

Python Program to Remove Duplicate Elements From a Sorted Array

Below is the Python program to remove duplicate elements from a sorted array:

        # Python program to remove duplicate elements from a sorted array
def removeDuplicateElements(arr, size):
    j = 0
    for i in range(size-1):
        if arr[i] != arr[i+1]:
            arr[j] = arr[i]
            j = j+1

    arr[j] = arr[size-1]
    j = j+1

    return j

def printListElements(arr, size):
    for i in range(size):
        print(arr[i], end=" ")
    print()

arr1 = [1, 1, 1, 2, 4, 6, 8, 8, 9, 9]
size1 = len(arr1)
print("Initial Array:")
printListElements(arr1, size1)
print("Array after removing duplicates:")
size1 = removeDuplicateElements(arr1, size1)
printListElements(arr1, size1)

arr2 = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
size2 = len(arr2)
print("Initial Array:")
printListElements(arr2, size2)
print("Array after removing duplicates:")
size2 = removeDuplicateElements(arr2, size2)
printListElements(arr2, size2)

arr3 = [10, 12, 12, 14, 16, 16, 18, 19, 19]
size3 = len(arr3)
print("Initial Array:")
printListElements(arr3, size3)
print("Array after removing duplicates:")
size3 = removeDuplicateElements(arr3, size3)
printListElements(arr3, size3)

Output:

        Initial Array: 
1 1 1 2 4 6 8 8 9 9
Array after removing duplicates:
1 2 4 6 8 9
Initial Array:
1 1 2 2 3 3 4 4 5 5
Array after removing duplicates:
1 2 3 4 5
Initial Array:
10 12 12 14 16 16 18 19 19
Array after removing duplicates:
10 12 14 16 18 19

Related: An Introduction to the Merge Sort Algorithm

JavaScript Program to Remove Duplicate Elements From a Sorted Array

Below is the JavaScript program to remove duplicate elements from a sorted array:

        // JavaScript program to remove duplicate elements from a sorted array

// Function to remove duplicate elements from a sorted array
function removeDuplicateElements(arr, size)
{
 let j = 0;

for (let i = 0; i < size-1; i++)
 {
 // If ith element is not equal to (i+1)th element,
 // then store ith value in arr[j]
 if (arr[i] != arr[i+1])
 {
 arr[j] = arr[i];
 j++;
 }
 }

// Storing the last value of arr in arr[j]
 arr[j++] = arr[size-1];

return j;
}

function printArrayElements(arr, size) {
 for(let i=0; i<size; i++) {
 document.write(arr[i] + " ");
 }
 document.write("
");
}


var arr1 = [1, 1, 1, 2, 4, 6, 8, 8, 9, 9];
var size1 = arr1.length;
document.write("Initial Array: " + "
");
printArrayElements(arr1, size1);
document.write("Array after removing duplicates: " + "
");
size1 = removeDuplicateElements(arr1, size1);
printArrayElements(arr1, size1);

var arr2 = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5];
var size2 = arr2.length;
document.write("Initial Array: " + "
");
printArrayElements(arr2, size2);
document.write("Array after removing duplicates: " + "
");
size2 = removeDuplicateElements(arr2, size2);
printArrayElements(arr2, size2);

var arr3 = [10, 12, 12, 14, 16, 16, 18, 19, 19];
var size3 = arr3.length;
document.write("Initial Array: " + "
");
printArrayElements(arr3, size3);
document.write("Array after removing duplicates: " + "
");
size3 = removeDuplicateElements(arr3, size3);
printArrayElements(arr3, size3);

Output:

Related: How to Find the Most Frequently Occurring Character in a String

        Initial Array: 
1 1 1 2 4 6 8 8 9 9
Array after removing duplicates:
1 2 4 6 8 9
Initial Array:
1 1 2 2 3 3 4 4 5 5
Array after removing duplicates:
1 2 3 4 5
Initial Array:
10 12 12 14 16 16 18 19 19
Array after removing duplicates:
10 12 14 16 18 19

Practice String and Array Problems for Your Next Interview

String and array problems are among the most asked topics in technical interviews.

If you're looking to be as prepared as possible, you must practice some commonly asked problems like how to check if a string is a palindrome, how to check if a string is an anagram, find the most frequently occurring character in a string, how to reverse an array, sorting and searching algorithms based on arrays, how to reverse a string, etc.