Q. Write a program to delete duplicate elements from an array.



Delete Duplicate Elements :- In this, we will perform the delete operation in the array and after performing the delete operation, the array must contain only unique integer values.
For Example :-
Input = 10, 20, 10, 1, 100, 10, 2, 1, 5, 10
Output = 10, 20, 1, 100, 2, 5
So as we can see that the input consists of 10 elements (10, 20, 10, 1, 100, 10, 2, 1, 5, 10) which contain some duplicate elements. So we have to remove duplicate elements from the array, and after performing the delete operation the output will be 10, 20, 1, 100, 2, 5.

Delete Duplicate Elements Algorithm

START
Step 1 -> Input the number of elements of the array.
Step 2 -> Input the array elements.
Step 3 -> Repeat from i = 1 to n
- if (arr[i] != arr[i+1])
- temp[j++] = arr[i]
- temp[j++] = arr[n-1]
Step 4 -> Repeat from i = 1 to j
- arr[i] = temp[i]
Step 5 -> return j.
STOP


Program To Delete Duplicate Elements From An Array

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main()
    {
    int arr[10]={10, 20, 10, 1, 100, 10, 2, 1, 5, 100}; 
    int size=10;         
    int i, j, k;       // Loop control variables
    /*
    * 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; 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("Array elements after deleting duplicates : ");
    for(i=0; i< size; i++)
    {
    printf("%d ", arr[i]);
    }
    return 0;
    }
    
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    int i,j,k,size=10,arr[30]={10, 20, 10, 1, 100, 10, 2, 1, 5, 100};
    for(i=0;i< size;++i)
    for(j=i+1;j< size;)
    {
    if(arr[i]==arr[j])
    {
    for(k=j;k< size-1;++k)
    arr[k]=arr[k+1];
    --size;
    }
    else
    ++j;
    }
    cout<<"Array elements after deleting duplicates : ";
    for(i=0;i< size;++i)
    cout<< arr[i]<<" ";
    return 0;
    }
    
    import java.util.Arrays;
    import java.util.LinkedHashSet;
    public class Main 
    {
    public static void main(String[] args) throws CloneNotSupportedException 
    {
    //Array with duplicate elements
    Integer[] numbers = new Integer[] {10, 20, 10, 1, 100, 10, 2, 1, 5, 100};
    //Create set from array elements
    LinkedHashSet< Integer > linkedHashSet = new LinkedHashSet< >( Arrays.asList(numbers) );
    //Get back the array without duplicates
    Integer[] numbersWithoutDuplicates = linkedHashSet.toArray(new Integer[] {});
    System.out.print("Array elements after deleting duplicates : ");
    //Verify the array content
    System.out.println( Arrays.toString(numbersWithoutDuplicates) );
    }
    }
    
    mylist = [10, 20, 10, 1, 100, 10, 2, 1, 5, 100]
    mylist = list(dict.fromkeys(mylist))
    print("Array elements after deleting duplicates : ")
    print(mylist)
    
    using System;
    using System.Linq;
    class Program
    {
    static void Main(string[] args)
    {
    int[] ia = {10, 20, 10, 1, 100, 10, 2, 1, 5, 100};
    int[] ia2 = ia.Distinct().ToArray();
    Console.Write("Array elements after deleting duplicates : ");
    foreach(int i in ia2) Console.Write("{0} ", i);
    Console.WriteLine(); 
    Console.ReadKey();
    }
    }
    
    $arr = array(10, 20, 10, 1, 100, 10, 2, 1, 5, 100);
    $result = array_unique($arr);
    print("Array elements after deleting duplicates : ");
    foreach ($result as $value) {
    print_r($value);
    echo " ";
    }
    
    #include <stdio.h>
    int main()
    {
    int arr[10]={10, 20, 10, 1, 100, 10, 2, 1, 5, 100}; 
    int size=10;         
    int i, j, k;       // Loop control variables
    /*
    * 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; 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("Array elements after deleting duplicates : ");
    for(i=0; i< size; i++)
    {
    printf("%d ", arr[i]);
    }
    return 0;
    }
    

    Output

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

    Recommended Programs

       Program to print lower triangular matrix.
       Program to subtract two matrices.