Remove Duplicate Elements from an Array in C

This section will discuss the removing or deletion of the duplicate elements from an array in the C programming language. When the same number of elements occurs in sorted or unsorted array, then the elements of the array is called the duplicate elements. And we need to delete these duplicate elements or same number from an array to make the resultant array consists of unique elements.

For example, there is an integer type array arr[10] that contains { 5, 8, 3, 3, 5, 9} elements. In this array, 3 and 5 occurs two times. Hence, these elements are duplicate elements. So, after deleting the duplicate elements from an array arr[], we get 5, 8, 3,9 elements.

Steps to delete the duplicate elements from unsorted array

Step 1: Input the size of an array from the user and store into the size variable.

Step 2: Use for loop to read the elements of an array and store in arr[i] variable.

Step 3: To get the duplicate elements from an array we need to use two for loops. Where the first loop start from 0 to size. And the structure of the loop is: for (i = 0; i < size; i++).

Another loop selects each element of the array and compare with the corresponding element to get the duplicate elements. And the structure of the inner loop is: for (j = i + 1; j < size; j++) and the code to find the subsequent same element is: if (arr[i] == arr[j]).

Step 4: If any duplicate element is encountered, delete the duplicate element from an array and the size of array is decrement by 1 such that, size = size - 1.

Step 5: After that, print the unique elements of an array and the code is:

Example 1: Program to remove the duplicate elements from an array

Let's consider an example to delete the same number or duplicate elements from an array in the C programming language.

When we execute the above program in C compiler, it produces the given below output in the console screen.

Define the number of elements in an array: 10

 Enter 10 elements of an array:
 57
12
89
32
62
12
89
35
67
75

 Array elements after deletion of the duplicate elements:  57    12      89      32      62      35      67      75

Remove the Duplicate Elements from Sorted Array

Suppose we have a given sorted array and the task is to remove the same number of elements from the array. For example, there is an integer type array whose size is 5 and contains arr[] = {2, 2, 3, 3, 5} elements. In this sorted array, 2 and 3 numbers occurs two times. Hence, these elements are duplicate elements. So, after deleting the duplicate elements from an array arr[], we get 2, 3, 5 elements and the new size of array is 3.

Algorithm to delete the duplicate elements from sorted array

Following is the algorithm to delete the duplicate array's elements from the sorted array in the C programming language.

  1. Define the size of elements of the array.
  2. Read the array elements from the user.
  3. Repeat from i = 1 to num.
    1. if (arr[i] != arr [i + 1]
    2. temp [j++] = arr[i]
    3. temp [j++] = arr[n- 1]
    4. Repeat from i = 1 to j
    5. arr[i] = temp[i]
    6. arr [i] = temp [i]
    7. Return j.
  4. Print unique elements of the array.

Example 2: Program to remove the duplicate elements from sorted array using the user-defined function

Let's consider an example to delete the same number or duplicate elements from sorted array in the C programming language.

When we execute the above program in C compiler, it produces the given below output in the console screen.

Define the no. of elements of the array: 10
 Enter the elements: 5
6
6
7
8
8
9
10
11
11

 Elements before removing duplicates:  5 6 6 7 8 8 9 10 11 11
 Display array's elements after removing duplicates:  5 6 7 8 9 10 11

Example 3: Program to remove the duplicate array elements using pointer

Let's consider an example to print the unique array elements by removing the duplicate element using the pointers in the C programming language.

When we execute the above program in C compiler, it produces the given below output in the console screen.

Define the size of the array element: 10

 Enter the elements of the array:
5
5
6
7
7
8
8
9
10
11
 After removing the duplicate elements:  5       11      6       7       10      8       9

Example 4: Delete Duplicate Elements from a sorted array by creating separate index for same elements

Let's consider an example to display the unique elements from a sorted array by creating extra space for same number of element/ index in the C programming language.

When we execute the above program in C compiler, it produces the given below output in the console screen.

Display the unique elements from the sorted array:
  5      10      15      20      25      30





Latest Courses