Delete all duplicate elements from an array with C.


Example

Input

Input array elements: 1 2 3 4 5 1 2 4 7 8

Output

After removing all duplicate elements
Elements of array are:
1 2 3 4 5 7 8


#include <stdio.h>
int main()
{
    int arr[100];      // 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 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 - 1; 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 : 1 2 3 4 5 1 2 4 7 8

Array elements after deleting duplicates : 1 2 3 4 5 7 8