C Program to Delete Duplicate Elements from an Array

  • Write a C program to delete duplicate elements of unsorted array
  • Write a C program to print unique elements of an array

Given an array of length N, which may contain some duplicate elements. We have to remove all duplicate elements and print only unique elements in array. If an element is present more than once in input array then output array should contain only one one instance of that element.

For Example
Input Array: 6 3 3 5 8 6
Output Array: 6 3 5 8

Algorithm to delete duplicate elements from an array
Let inputArray is an array of length N, and readIndex and writeIndex are two integer variables to store index references.
  • readIndex scan elements from left to write.

  • At any instant of time all the elements before writeIndex are unique.

  • We initialize readIndex and writeIndex with zero and start traversing the array.

  • For any element A at index i, we scan remaining elements of array from index i+1 to N-1. If we found one more A then we skip A at index i, otherwise A is unique and we copy it in out unique element list(inputArray[writeIndex]).
  • At the end of traversal, we will get all unique elements between index 0 to writeIndex.
Time Complexity : O(n2)

C program to delete duplicate elements from an array

Below program defines three integer variables(readIndex, writeIndex and scanIndex) to store indexes of input array. Let the number of elements in array is N.

  • All the elements before writeIndex are unique.
  • readIndex traverse the array from index 0 to N-1 and for every element it checks whether it is unique or not
  • scanIndex traverse the array from readIndex+1 to N-1. It tries to find the duplicate element of array[readIndex]
First of all, below program takes an integer array as input from user. Then for every element of array it check whether a duplicate element is present in array. If a duplicate element is found then it skips that element otherwise copy that element at writeIndex and increment writeIndex. At the end of the scan, all the elements before writeIndex are unique and number of unique elements in array is equal to the value of writeIndex.

/*
* C Program to delete duplicate elements 
* from an unsorted array
*/
#include <stdio.h>
#include <conio.h>

int main(){
    int inputArray[500], elementCount, counter;
    int readIndex, writeIndex, scanIndex;
    
    printf("Enter number of elements in array: ");
    scanf("%d", &elementCount);
        
    printf("Enter %d numbers \n", elementCount);
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }    
    /*
     * All the elements before writeIndex are unique.
     * readIndex scan elements from left to write and
     * tries to find a duplicate element. 
     */
    for(readIndex=0, writeIndex=0; readIndex < elementCount; readIndex++){
        for(scanIndex=readIndex+1; scanIndex < elementCount; scanIndex++){
            if(inputArray[scanIndex] == inputArray[readIndex]){
                /* We found a duplicate element*/
                break;
            }
        }
        if(scanIndex == elementCount){
        /* We scanned whole array but didn't found any duplicate array */
            inputArray[writeIndex] = inputArray[readIndex];
            writeIndex++;
        }
    }
    
    /* Print unique element */
    printf("Unique Elements\n");
    for(counter = 0; counter < writeIndex; counter++){
        printf("%d ", inputArray[counter]);
    } 
        
    getch();
    return 0;
}
Program Output
Enter number of elements in array: 7
1 6 2 4 1 6 3
Unique Elements
2 4 1 6 3

Related Topics
C program to delete duplicate elements from a sorted array
C Program to find maximum elements in an array
C program to find second largest element in array
C program to remove vowels from a string
C Program to find frequency of characters in a string
C Program to find sum of diagonal elements of matrix
C program to find sum of array elements using recursion
C program to check if two strings are anagram
List of all C Programs