C Program to remove duplicate elements from an array

In this tutorial, we will write a simple c program to remove duplicate elements from an array. This c program works for both sorted or unsorted arrays.

In order to remove duplicate elements from an array, we need to perform two operations, first, check if any element on the ith index of an array is equal to any other element in the array. If duplicate element found, remove it by shifting all other elements in the towards on lower index.

For example, we have this array { 3,1,2,2,4,4,5 } and there are two duplicate elements 2 and 4. Both of these elements occur two times. We start scanning from first elements of the array and if duplicate found we remove the duplicate by shifting all other elements on index towards left.

C Program

#include<stdlib.h>
#include<stdio.h>
int remove_duplicate(int arr[],int size)
{
	
	for (int i = 0; i < size; i++)
	{
		for (int j = i; j < size; j++)
		{
			if (arr[i] == arr[j+1])
			{
				for (int k = j; k < size; k++)
				{
					arr[k] = arr[k + 1];
				}
				size--;
			}
		}
	}
	return size;
}
int main()
{
	int arr[] = { 3,1,2,2,4,4,5 };
	int size = remove_duplicate(arr,7);
	printf("Print the array after remove duplicates\n");
	for (int i = 0; i < size; i++)
	{
		printf("%d", arr[i]);
	}
	return 0;
}

Code Output

Print the array after remove duplicates
31245

How Code Works?

First, we define an array of 7 elements.

int arr[] = { 3,1,2,2,4,4,5 };

This line calls a function name “remove_duplicates”. This function takes array to be sorted and number of array elements as input arguments and return the size of array after deleting duplicate elements.

int size = remove_duplicate(arr,7);

Now let’s see the working of remove_duplicate() function. This is used to scan all elements or you can say to compare every element of an array with all other elements of the array.

First ‘for’ loop picks all elements one by one starting from 0th index and compares it with all other elements of the array with the help of the second ‘for’ loop.

for (int i = 0; i < size; i++)

For example, at the start, first for loop select element on 0th index and compare it with other elements of the array using the second for loop.

Inside the 2nd for loop if any element is equal to a selected element of the 0th index ( if condition is used to check this), then we will delete it by shifting all remaining elements of the array by 1 index towards left

for (int j = i; j < size; j++)
if (arr[i] == arr[j+1]) // check if equal
//if equal, move all elements towards left by one position
for (int k = j; k < size; k++)
{
arr[k] = arr[k + 1];
}

Similarly, on the second execution of first for loop, the value of ‘i’ will become one and now function compares the array element on the 1st index with all other elements of the array. If the first index element is equal to any other element of the array, we will remove the duplicate element by shifting the remaining elements of the array towards left by one.

This pattern keeps executing a number of times the size of elements of the array and at the end, all duplicate elements will be removed and we will get an array with all unique elements only.

There is one more thing in this code. we will return the size of the array through this function. Because it will define the remaining elements left in the array after removing duplicate elements and will use size value to print the number of elements left in the array.

After returning the value, print elements with the help of printf() function.

printf("Print the array after remove duplicates\n");
	for (int i = 0; i < size; i++)
	{
		printf("%d", arr[i]);
	}
  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type
  5. In which order function parameters are evaluated in C/C++?
  6. Scope of global and static variables in C/C++
  7. Difference between Structure and Union in C
  8. C Program to remove duplicate elements from an array

Leave a Reply