An **array** is a collection of similar data elements stored in a contiguous memory location.

**Example:** arr[5] = {2,7,1,23,5}

**Example:**

**Input Array:** 1,2,4,5,4,2,7,5

**Output:** Resultant Array after removing duplicates: 1,2,4,5,7

Write a C Program to remove duplicate elements from an Array.

1. Take size of the array n as input from user.

2. Initialize an array arr of size n.

3. Enter the elements into the array.

4. Run two for loops and check for unique elements.

5. Store all the unique elements in another array temp.

6. Print all the elements inside the array temp.

7. Exit.

There are several ways to remove duplicate elements from an array in C language. Let’s take a detailed look at all the approaches to remove duplicate elements from an array.

- Remove Duplicate Elements from an Array using Nested For Loop
- Remove Duplicate Elements from an Array using Sort Function with Extra Space
- Remove Duplicate Elements from an Array using Sort Function without Extra Space

In this approach, we remove duplicate elements from array using nested for loop.

Here is the source code of the C program to remove duplicate elements from array using nested for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to remove duplicates from array using nested for loop`

`*/`

`#include <stdio.h>`

int main()

`{`

int n, count = 0;

printf("Enter number of elements in the array: ");

scanf("%d", &n);

int arr[n], temp[n];

if(n==0)

`{`

printf("No element inside the array.");

exit(0);

`}`

printf("Enter elements in the array: ");

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

`{`

scanf("%d", &arr[i]);

`}`

printf("\nArray Before Removing Duplicates: ");

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

printf("%d ", arr[i]);

`// To store unique elements in temp after removing the duplicate elements`

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

`{`

int j;

for (j = 0; j < count; j++)

`{`

if (arr[i] == temp[j])

break;

`}`

if (j == count)

`{`

temp[count] = arr[i];

`count++;`

`}`

`}`

printf("\nArray After Removing Duplicates: ");

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

printf("%d ", temp[i]);

return 0;

`}`

1. Take the number, **n** as input from the user.

2. Declare an array of size **n**.

3. Enter **n** elements into the array.

4. If **n = 0** means there is no element in the array. Print there is no element in the array and exit.

5. Print the array before removing the duplicates.

6. Run a nested loop first one is to copy the element from the array to **temp** array.

7. If that element is existing already in **temp** variable, then we will break the inner loop and continue executing for remaining elements the same process.

8. After coming out of the loop all the unique elements have been stored in the temp array.

9. Print the elements in the temp array, this is the array after removing the duplicates from the array.

10. Exit.

**Example: (Step by Step Procedure to Remove Duplicates from an Array)**

Let the array be of size 6 and array elements be 3, 2, 3, 4, 2, 4 i.e.,

arr =

3 | 2 | 3 | 4 | 2 | 4 |

Initialize another array temp of size 6 to store all the unique elements and a count variable to keep count of elements in temp array.

**Step 1:**

Store the 1st element of arr to temp array and make count=1.

temp =

3 |

Run the for loop from i = 1 to n and in each step check if the element is already present in the temp array. If element is already present in the temp array skip to the next element and if element is not present in the temp array insert the element in the temp array and increase the value of count by 1.

**Step 2:**

For i=1, arr[1] = 2. Run another for loop from j=0 to count and check if arr[i] element is present or not. Since count = 1, inner for loop will run only once, and element in the temp[0] = 3. Since, temp[0] not equal to arr[1] we will insert 2 in the temp array and count = 2.

temp =

3 | 2 |

**Step 3:**

For i=2, arr[2] = 3. Run another for loop from j=0 to count=2 and check if arr[i] element is present or not. temp[0] = 3, which is equal to arr[2] i.e., element is already present in temp array so we will break the inner for loop and increment the value of i by 1 in the outer loop, i.e. i=3.

**Step 4:**

For i=3, arr[3] = 4. Run another for loop from j=0 to count=2 and check if arr[i] element is present or not. Inside temp array we have 3 and 2 which is not equal to 4 so we will insert 4 in the temp array and increment count by 1 i.e., count = 3.

temp =

3 | 2 | 4 |

**Step 5:**

For i=4, arr[4] = 2. Run inner for loop from j=0 to count=3 and check if arr[i] element is present or not. temp[1] = 2, which is equal to arr[4] i.e., element is already present in temp array so we will break the inner for loop and increment the value of i by 1 in the outer loop, i.e. i=5.

**Step 6:**

For i=5, arr[5] = 4. Run another for loop from j=0 to count=3 and check if arr[i] element is present or not. temp[2] = 4, which is equal to arr[5] i.e., element is already present in temp array so we will break the inner for loop and increment the value of i by 1 in the outer loop, i.e. i=6.

**Step 7:**

Now, i=6 i.e., i is not less than n, therefore exit the outer for loop as entire array has been traversed. Now, run a for loop from j=0 to count and print all the elements in the temp array. These are the elements that is there in the array after removing all the duplicates.

Therefore, elements in the array after removing the duplicates are: 3, 2, 4.

**Time Complexity: O(n ^{2})**

The above program for removing duplicates from an array has a time complexity of O(n^2) as there is a nested for loop which runs for n number of times.

**Space Complexity: O(n)**

In the above program, space complexity is O(n) as arrays arr[] and temp[] of size n has been initialized to store elements.

**Testcase 1:** In this case, the array has “8” elements, and the array elements are “9, 3, 6, 9, 5, 4, 0, and 5”.

Enter number of elements in the array: 8 Enter elements in the array: 9 3 6 9 5 4 0 5 Array Before Removing Duplicates: 9 3 6 9 5 4 0 5 Array After Removing Duplicates: 9 3 6 5 4 0

**Testcase 2:** In this case, the array has “5” elements, and the array elements are “3, 2, 3, 3, and 2”.

Enter number of elements in the array: 5 Enter elements in the array: 3 2 3 3 2 Array Before Removing Duplicates: 3 2 3 3 2 Array After Removing Duplicates: 3 2

In this approach, we remove duplicate elements from array using sort function with extra space.

Here is the source code of the C program to remove duplicate elements from array using sort function with extra space. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to remove duplicates from array using sort function with extra space`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

`//function cmpfunc to compare two numbers`

int cmpfunc(const void *a, const void *b)

`{`

return(*(int*)a - *(int*)b);

`}`

`//main function`

int main()

`{`

int n;

printf("Enter number of elements in the array: ");

scanf("%d",&n);

int arr[n];

printf("Enter elements in the array: ");

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

scanf("%d",&arr[i]);

`//if array is empty`

if(n==0)

`{`

printf("No element inside the array.");

exit(0);

`}`

printf("\nArray Before Removing Duplicates: ");

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

printf("%d ", arr[i]);

`//inbuilt function qsort to sort the array`

qsort(arr,n,sizeof(int),cmpfunc);

`//temporary array to store only unique elements`

int temp[n];

int i = 0, j = 0;

temp[j++] = arr[i++];

`//loop to remove duplicate elements and store`

`//unique elements in the array temp`

for( ; i<n; i++)

`{`

if(arr[i] != arr[i-1])

`{`

temp[j++] = arr[i];

`}`

`}`

`//print the array after removing all the duplicate elements`

printf("\nArray After Removing Duplicates: ");

for(i=0;i<j;i++)

printf("%d ",temp[i]);

return 0;

`}`

1. Take the number, **n** as input from the user.

2. Declare an array of size **n**.

3. Enter **n** elements into the array.

4. If **n = 0** means there is no element in the array. Print there is no element in the array and exit.

5. Print the array elements before removing the duplicates.

6. Sort the array using the function **qsort()**.

7. The values that needs to be passed on the function **qsort()** are: **arr, n, sizeof(int)** and call to a function **cmpfunc**.

8. The function **cmpfunc** is used to compare two values at a time in an array.

9. After sorting the array initialize another array temp of size **n**.

10. Run a for loop till n and in each iteration check if **arr[i]** is equal to **arr[i-1]** or not.

11. If **arr[i]** is not equal to **arr[i-1]**, enter the element at arr[i] to the array temp.

12. Repeat this step till **i=n**.

13. Now, the array temp contains all the unique values in a sorted order.

14. print all the elements present inside the array temp.

15. Exit.

**Time Complexity: O(n log(n))**

The above program for removing duplicates from an array has a time complexity of O(n log(n)) as the time complexity of qsort function i.e., quicksort is O(n log(n)).

**Space Complexity: O(n)**

In the above program, space complexity is O(n) as arrays arr[] and temp[] of size n has been initialized to store elements.

**Testcase 1:** In this case, the array has “8” elements, and the array elements are “9, 3, 6, 9, 5, 4, 0, and 5”.

Enter number of elements in the array: 8 Enter elements in the array: 9 3 6 9 5 4 0 5 Array Before Removing Duplicates: 9 3 6 9 5 4 0 5 Array After Removing Duplicates: 0 3 4 5 6 9

**Testcase 2:** In this case, the array has “5” elements, and the array elements are “3, 2, 3, 3, and 2”.

Enter number of elements in the array: 5 Enter elements in the array: 3 2 3 3 2 Array Before Removing Duplicates: 3 2 3 3 2 Array After Removing Duplicates: 2 3

In this approach, we remove duplicate elements from array using sort function without extra space i.e., we will not use extra temp[], array to store the values. We will remove duplicates from the array arr[] itself without using any extra array.

Here is the source code of the C program to remove duplicate elements from array using sort function without extra space. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/*`

`* C Program to remove duplicates from array using sort function without extra space`

`*/`

`#include<stdio.h>`

`#include<stdlib.h>`

`//function cmpfunc to compare two numbers`

int cmpfunc(const void *a, const void *b)

`{`

return(*(int*)a - *(int*)b);

`}`

`//main function`

int main()

`{`

int n;

printf("Enter number of elements in the array: ");

scanf("%d",&n);

int arr[n];

printf("Enter elements in the array: ");

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

scanf("%d",&arr[i]);

`//if array is empty`

if(n==0)

`{`

printf("No element inside the array.");

exit(0);

`}`

printf("\nArray Before Removing Duplicates: ");

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

printf("%d ", arr[i]);

`//inbuilt function qsort to sort the array`

qsort(arr,n,sizeof(int),cmpfunc);

int j = 0;

for(int i=1; i<n; i++)

`{`

if(arr[i] != arr[i-1])

`{`

arr[++j]=arr[i];

`}`

`}`

printf("\nArray After Removing Duplicates: ");

for(int i=0;i<j+1;i++)

printf("%d ",arr[i]);

return 0;

`}`

1. Take the number, **n** as input from the user.

2. Declare an array of size **n**.

3. Enter **n** elements into the array.

4. If **n = 0** means there is no element in the array. Print there is no element in the array and exit.

5. Print the array elements before removing the duplicates.

6. Sort the array using the function **qsort()**.

7. The values that needs to be passed on the function **qsort()** are: **arr, n, sizeof(int)** and call to a function **cmpfunc**.

8. The function **cmpfunc** is used to compare two values at a time in an array.

9. Declare a variable **j = 0**.

10. Run a for loop from **i=1** to **n** and in each iteration check if **arr[i]** is equal to **arr[i-1]** or not.

11. If **arr[i]** is not equal to **arr[i-1]**, enter the element at arr[i] to **arr[++j]**.

12. Repeat this step till i=n.

13. Now, the array arr contains all the unique values in a sorted order.

14. print all the elements present inside the array **arr**.

15. Exit.

**Time Complexity: O(n log(n))**

The above program for removing duplicates from an array has a time complexity of O(n log(n)) as the time complexity of qsort function i.e., quicksort is O(n log(n)).

**Space Complexity: O(n)**

In the above program, space complexity is O(n) as array arr[] of size n has been initialized to store elements.

**Testcase 1:** In this case, the array has “8” elements, and the array elements are “9, 3, 6, 9, 5, 4, 0, and 5”.

Enter number of elements in the array: 8 Enter elements in the array: 9 3 6 9 5 4 0 5 Array Before Removing Duplicates: 9 3 6 9 5 4 0 5 Array After Removing Duplicates: 0 3 4 5 6 9

**Testcase 2:** In this case, the array has “5” elements, and the array elements are “3, 2, 3, 3, and 2”.

Enter number of elements in the array: 5 Enter elements in the array: 3 2 3 3 2 Array Before Removing Duplicates: 3 2 3 3 2 Array After Removing Duplicates: 2 3

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**Related Posts:**

- Apply for C Internship
- Practice Computer Science MCQs
- Check C Books
- Apply for Computer Science Internship
- Check Computer Science Books