C program to delete all duplicate elements from an array

Write a C program to delete(remove) all duplicate elements from an array. C program to remove all duplicate integer values from an array. After performing delete operation the array should only contain unique integer value. Logic to delete all duplicate elements from a given array.

Example

Input

Input array elements: 10, 20, 10, 1, 100, 10, 2, 1, 5, 10

Output

After removing all duplicate elements
Elements of array are: 10, 20, 1, 100, 2, 5

Required knowledge

Basic C programming, If else, For loop, Nested loop, Array

Must know -

Logic to remove duplicate elements from array

Below is the step by step descriptive logic to remove duplicate elements from an array.

  1. Read elements in an array, say arr contains all array elements.
  2. To find duplicate elements in a given array we need two loops. Run an outer loop loop from 0 to N (where N is size of the array). The loop structure must look like for(i=0; i<N; i++). This loop is used to select each element of array and check next subsequent elements for duplicates using another nested loop.
  3. Run another inner loop to find first duplicate of current element i.e. arr[i]. Run an inner loop from i + 1 to N, the loop structure must look like for(j=i+1; j<N; j++).
  4. Inside the inner loop check for duplicate element. If a duplicate element is found then remove the current duplicate element. Also if a duplicate element is found the decrement size of the array i.e. N = N - 1.

Read more - Program to print unique elements in an array

Program to remove duplicate elements from array

/**
 * C program to delete all duplicate elements from array
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the array

int main()
{
    int arr[MAX_SIZE]; // Declares an array of size 100
    int size; // Total number of elements in array
    int i, j, k; // Loop control variables

    /* Input size of the array */
    printf("Enter size of the array : ");
    scanf("%d", &size);

    /* Input elements in the array */
    printf("Enter elements in array : ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &arr[i]);
    }


    /*
     * Find all duplicate elements in array
     */
    for(i=0; i<size; i++)
    {
        for(j=i+1; j<size; j++)
        {
            /* If any duplicate found */
            if(arr[i] == arr[j])
            {
                /* Delete the current duplicate element */
                for(k=j; k<size; k++)
                {
                    arr[k] = arr[k + 1];
                }

                /* Decrement size after removing duplicate element */
                size--;

                /* If shifting of elements occur then don't increment j */
                j--;
            }
        }
    }


    /*
     * Print array after deleting duplicate elements
     */
    printf("\nArray elements after deleting duplicates : ");
    for(i=0; i<size; i++)
    {
        printf("%d\t", arr[i]);
    }

    return 0;
}

Output

Enter size of the array : 10
Enter elements in array : 10 20 10 1 100 10 2 1 5 10

Array elements after deleting duplicates : 10    20    1    100    2    5

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>