[C Language] How to Remove Duplicate Item In An Array

Working with array, its little complicated then other programs but you should learn how to work with at least 2 dimensional array. In this tutorial, we will check how we can remove duplicate items in an array.

[C Language] How to Remove Duplicate Item In An Array

How to Remove Duplicate Item In An Array

For example, we have an int type array that contain some duplicate elements. arr[10] = [5, 8, 3, 3, 2, 2, 5]. In this array, we have 2, 3 and 5 that repeated multiple times. so after remove the elements we will have [5, 8, 3, 2] array.

This is whole idea to remove duplicate element from an array. Its better if you understand the problem fully so you can design a better solution.

We will follow some step that help us to remove all duplicate items from a unsorted array.

Steps:

  1. First of all we need a size of array from user input then we store it into a variable
  2. After that we will use loop to read every element of array and store in arr[i] variable.
  3. To get the duplicate element from an array we will need extra loop which check the duplicate element (j = i + 1; j < size; j++).
  4. now we have two loops that iterating element so we can easily look up for duplicate element with conditional statements.
  5. if there any duplicate element found we delete that element and reduce the size of array by 1 on every duplicate element.
  6. After that print out all values that are unique in array.
CCopy
    #include <stdio.h>  
    #include <conio.h>  
    int main ()  
    {  
        // declare local variables   
        int arr[20], i, j, k, size;  
          
        printf (" Define the number of elements in an array: ");  
        scanf (" %d", &size);  
          
        printf (" \n Enter %d elements of an array: \n ", size);  
        // use for loop to enter the elements one by one in an array  
        for ( i = 0; i < size; i++)  
        {  
            scanf (" %d", &arr[i]);  
        }  
          
          
        // use nested for loop to find the duplicate elements in array  
        for ( i = 0; i < size; i ++)  
        {  
            for ( j = i + 1; j < size; j++)  
            {  
                // use if statement to check duplicate element  
                if ( arr[i] == arr[j])  
                {  
                    // delete the current position of the duplicate element  
                    for ( k = j; k < size - 1; k++)  
                    {  
                        arr[k] = arr [k + 1];  
                    }  
                    // decrease the size of array after removing duplicate element  
                    size--;  
                      
                // if the position of the elements is changes, don't increase the index j  
                    j--;      
                }  
            }  
        }  
          
          
        /* display an array after deletion or removing of the duplicate elements */  
        printf (" \n Array elements after deletion of the duplicate elements: ");  
          
        // for loop to print the array  
        for ( i = 0; i < size; i++)  
        {  
            printf (" %d \t", arr[i]);  
        }  
        return 0;  
    }  

Quick and easy, right. We have many of these type of example on our website check more article and increase your coding knowledge. and let us know if you need any article on some topic. define your topic and we will try our best to describe you in simple language. Happy Coding. . .

Leave a Reply

Your email address will not be published. Required fields are marked *