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:

Initialize a hash map that’ll store all the unique elements of the array. Traverse the array. Check if the element is present in the array. If the element is present in the array, keep traversing. 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:

Output:

Python Program to Remove Duplicate Elements From an Unsorted Array

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

Output:

JavaScript Program to Remove Duplicate Elements From an Unsorted Array

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

Output:

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:

Initialize the index variables i and j with 0. Iterate the array. 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. Increment the value of i in each iteration. Store the last value of arr in arr[j]. 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:

Output:

Python Program to Remove Duplicate Elements From a Sorted Array

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

Output:

JavaScript Program to Remove Duplicate Elements From a Sorted Array

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

Output:

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.