Remove Duplicate Elements From An Array In C

To eliminate duplicate elements from an unsorted array, follow these steps:

  1. Create an empty hash set to store unique elements.
  2. Iterate through the array elements one by one.
  3. For each element, check if it already exists in the hash set.
  4. If the element is not in the set, add it to the set.
  5. If the element is already in the set, it is a duplicate, so remove it from the array.
  6. After iterating through all elements, the array will contain only unique elements.

Step 1: Accept the array size input from the user and save it in the variable named size.

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

To identify duplicate elements within an array, it is necessary to employ a pair of nested for loops. The initial loop commences from 0 up to the size of the array. The syntax for this loop is as follows: for (i = 0; i < size; i++).

A different loop iterates through each item in the array and compares it with the matching item to identify duplicates. The format of the nested loop is defined as: for (j = i + 1; j < size; j++). The logic used to detect identical elements further in the array is: if (arr[i] == arr[j]).

If a duplicate element is found, it is removed from the array, reducing the array size by 1 as a result, where size = size - 1.

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

Example

for (i = 0; i < size; i++)
{
printf ("%d \t" , arr[i]);
}

Example 1: Algorithm to eliminate duplicate items from an array

Let's explore an illustration on removing duplicate elements with the same value from an array in the C programming language.

Example

#include <stdio.h>
#include <conio.h>
int main ()
{
	// declare local variables 
	int arr[20], i, j, k, size;
	
	printf (" Define the number of elements in an array: ");
	scanf (" %d", &size);
	
	printf (" \n Enter %d elements of an array: \n ", size);
	// use for loop to enter the elements one by one in an array
	for ( i = 0; i < size; i++)
	{
		scanf (" %d", &arr[i]);
	}
	
	
	// use nested for loop to find the duplicate elements in array
	for ( i = 0; i < size; i ++)
	{
		for ( j = i + 1; j < size; j++)
		{
			// use if statement to check duplicate element
			if ( arr[i] == arr[j])
			{
				// delete the current position of the duplicate element
				for ( k = j; k < size - 1; k++)
				{
					arr[k] = arr [k + 1];
				}
				// decrease the size of array after removing duplicate element
				size--;
				
			// if the position of the elements is changes, don't increase the index j
				j--;	
			}
		}
	}
	
	
	/* display an array after deletion or removing of the duplicate elements */
	printf (" \n Array elements after deletion of the duplicate elements: ");
	
	// for loop to print the array
	for ( i = 0; i < size; i++)
	{
		printf (" %d \t", arr[i]);
	}
	return 0;
}

When we run the aforementioned program in a C compiler, it generates the following output on the console screen.

Example

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

Given an ordered array, the objective is to eliminate an equal number of elements from it. Consider an array of integers with a size of 5, comprising elements arr = {2, 2, 3, 3, 5}. Within this ordered array, the numbers 2 and 3 are repeated twice, indicating duplicate elements. Consequently, upon removing these duplicate elements from the array arr, we are left with the elements 2, 3, 5, resulting in a reduced array size of 3.

An algorithm to remove duplicate elements from a sorted array can be implemented as follows:

  1. Initialize a variable to keep track of the index where the next non-duplicate element should be placed.
  2. Iterate through the sorted array starting from the second element.
  3. Compare the current element with the previous element.
  4. If the current element is different from the previous element, increment the index and copy the current element to the index.
  5. Continue this process until reaching the end of the array.
  6. The new length of the array (index + 1) will be the number of unique elements.
  7. Return the updated array with unique elements up to the new length.

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

  • Define the size of elements of the array.
  • Read the array elements from the user.
  • Repeat from i = 1 to num. 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] arr [i] = temp [i] Return j.
  • Print unique elements of the array.
  • 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]
  • arr [i] = temp [i]
  • Return j.

A sample code snippet that eliminates duplicate elements from a sorted array by leveraging a custom-defined function is as follows:

Let's explore an illustration on how to eliminate identical or duplicate elements with the same value from a sorted array in the C programming language.

Example

/* program to delete the duplicate elements from sorted array in C. */
#include <stdio.h>

int duplicate_element ( int arr[], int num)
{
	// check num is equal to 0 and num == 1
	if (num == 0 || num == 1)
		return num;
		
	// create temp array to store same number	
	int temp [num];	
	
	// declare variable
	int i, j = 0;
	
	// use for loop to check duplicate element
	for (i = 0; i < num - 1; i++)
	{
		// check the element of i is not equal to (i + 1) next element
		if (arr [i] != arr[i + 1])
			temp[j++] = arr[i];
	}
	temp[j++] = arr[ num - 1];


	// check the original array's elements with temporary array's elements
	for (i = 0; i < j; i++)
	{
		arr[i] = temp[i];
		}	
		
	return j;	
}

int main ()
{
	int num;
	printf (" Define the no. of elements of the array: ");
	scanf (" %d", &num);
	
	
	int arr[num], i;
	
	printf (" Enter the elements: ");
	// use loop to read elements one by one
	for ( i = 0; i < num; i++)
	{
		scanf (" %d", &arr[i]);
	}
	
	printf (" \n Elements before removing duplicates: ");
	for ( i = 0; i < num; i++)
	{
		printf (" %d", arr[i]);
	}
	
	num = duplicate_element (arr, num);
	
	// print array after removing duplicates elements
	printf (" \n Display array's elements after removing duplicates: ");
	for ( i = 0; i < num; i++)
	{
		printf (" %d", arr[i]);
		}	
		
	return 0;	
	
}

When the aforementioned program is run through a C compiler, it generates the following output on the console screen.

Example

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: Code to eliminate duplicate elements from an array using a pointer concept

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

Example

/* program to delete the duplicate elements from sorted array using pointers in C. */
#include <stdio.h>
#include <stdlib.h>

// declare variable
int i, j;

// definition of the function
int remove_element ( int *ptr, int num)
{
int temp;

// use for loop 

for ( i = 0; i < num-1; i++)
{
	for (j = i + 1; j < num; j++)
	{
		// if statement to check the same number int sorted array
		if ( *(ptr+i) == *(ptr+j))
		{
			temp = *(ptr + j);
			*(ptr + j) = *(ptr + num - 1);
			*(ptr + num - 1) = temp;
			num--;
		}
	}
}	
	
    printf (" After removing the duplicate elements: ");
	for ( i = 0; i < num; i++)
	{
		printf (" %d \t", *(ptr+i));
	}
	return 0;
}


int main ()
{
	// declare local variable
	int num, *ptr;
	printf (" Define the size of the array element: ");
	scanf (" %d", &num);
	
	ptr = (int *) malloc (num * sizeof(int));
	
	printf (" \n Enter the elements of the array: \n");
	// use for loop to read elements
	for ( i = 0; i < num; i++)
	{
		scanf (" %d", (ptr+i));
	}

	// call function to print the unique elements
	remove_element (ptr, num);
	return 0;
}

When running the aforementioned program in a C compiler, it generates the following output on the console screen.

Example

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

Delete Duplicate Elements from an ordered array by establishing a unique index for identical elements.

Let's explore an illustration demonstrating the distinct elements within a sorted array by allocating additional space equivalent to the number of elements or indices in the C programming language.

Example

#include <stdio.h>
#include <conio.h>

/* deleteDuplicate() function to remove same elements
and return new array size. */
int deleteDuplicate (int arr[], int num)
{
	
	if (num == 0 || num == 1)
		return num;


// store index of next unique element
int i, j = 0;

// use for loop to find and remove the duplicate elements
for (i = 0; i < num - 1; i++)
{
	if ( arr[i] != arr[i+1])
	{
		arr[j++] = arr[i];
	}	
	
}
arr[j++] = arr[num -1];
return j;
}


int main ()
{
	int i;
	// initialize sorted array
	int arr[] = {5, 10, 10, 15, 15, 20, 25, 25, 30, 30};
	
	// get the size of the array
	int num = sizeof(arr) / sizeof(arr[0]);
	
	/* call deleteDuplicate() function to 
	delete duplicate element and return new size. */
	num = deleteDuplicate(arr, num);
	
	printf (" Display the unique elements from the sorted array: \n ");
	// display the updated element of the sorted array
	for ( i = 0; i < num; i++)
	{
		printf (" %d \t", arr[i]);
	}
	return 0;
}

When the aforementioned program is run in a C compiler, it generates the following output on the console screen.

Example

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

Input Required

This code uses input(). Please provide values below: