Remove duplicate elements in an array

Problem statement:- Program to Remove duplicate elements in an array.

Data requirement:-

   Input Data:- arr,size

  Output Data:-arr

  Additional Data:- i, j, temp

Program in C

Here is the source code of the C Program to Remove duplicate elements in an array.

Code:

#include<stdio.h>
int sort_arr(int arr[],int size)
{
    int i,j,temp;
    //Sort the array
        for(i=0;i<size;i++)
        {
        for(j=i+1;j<size;j++)
        if(arr[i]>arr[j])
        {
         temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
        }}
}
main()
{
    printf("Enter the size of the array:");
    int size,i,j=0;
    scanf("%d",&size);
    int arr[size];
    printf("Enter the Element of the array:\n");
     for(i=0;i<size;i++)
       {
        scanf("%d",&arr[i]);
        }
        sort_arr(arr,size);

        //Remove duplicate element
        for(i=0;i<size-1;i++)
        {
            if(arr[i]!=arr[i+1])
            {
                arr[j++]=arr[i];

            }
        }
        arr[j++]=arr[size-1];
        printf("After removing duplicate element array is ");
        for(i=0;i<j;i++)
            printf("%d ",arr[i]);
}

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
5
6
7
5
After removing duplicate element array is 4 5 6 7

Program in C++

Here is the source code of the C++ Program to Remove duplicate elements in an array.

Code:

#include<iostream>
using namespace std;
int sort_arr(int arr[],int size)
{
    int i,j,temp;
    //Sort the array
        for(i=0;i<size;i++)
        {
        for(j=i+1;j<size;j++)
        if(arr[i]>arr[j])
        {
         temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
        }}
}
main()
{
    cout<<"Enter the size of the array:";
    int size,i,j=0,temp;
    cin>>size;
    int arr[size];
    cout<<"Enter the Element of the array:\n";
     for(i=0;i<size;i++)
       {
        cin>>arr[i];
        }
        sort_arr(arr,size);

        //Remove duplicate element
       for(i=0;i<size-1;i++)
        {
            if(arr[i]!=arr[i+1])
            {
                arr[j++]=arr[i];

            }
        }
        arr[j++]=arr[size-1];

        cout<<"After removing duplicate element from array: ";
        for(i=0;i<j;i++)
            cout<<arr[i]<<" ";
}

Input/Output:
Enter the size of the array:6
Enter the Element of the array:
52
37
52
39
52
36
After removing duplicate element from array: 36 37 39 52

Program in Java

Here is the source code of the Java Program to Remove duplicate elements in an array.

Code:

import java.util.Scanner;   
public class p27 {
static void sort_arr(int arr[],int size)
{
    int i,j,temp;
    //Sort the array
        for(i=0;i<size;i++)
        {
        for(j=i+1;j<size;j++)
        if(arr[i]>arr[j])
        {
         temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
        }}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int i,size,j=0;
    size=sc.nextInt();
    int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
     for(j=0;j<size;j++)
    {
        arr[j]=sc.nextInt();
    }
     sort_arr(arr,size);
     j=0;
     //Remove duplicate element
     for(i=0;i<size-1;i++)
     {
         if(arr[i]!=arr[i+1])
         {
             arr[j++]=arr[i];
         }
     }
     arr[j++]=arr[size-1];
     System.out.print("After removing duplicate element array is ");
     for(i=0;i<j;i++)
    System.out.print(arr[i]+" ");
    sc.close();
    }
}

Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
3
5
3
5
After removing duplicate element array is 3 5 

Program in Python

Here is the source code of the Python Program to Remove duplicate elements in an array.

Code:

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
arr.sort()
j=0
#Remove duplicate element
for i in range(0, size-1):
    if arr[i] != arr[i + 1]:
        arr[j]=arr[i]
        j+=1
arr[j] = arr[size - 1]
j+=1
print("After removing duplicate element array is")
for i in range(0, j):
    print(arr[i],end=" ")

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
4
52
89
700
52
After removing duplicate element array is
4 52 89 700 


Post a Comment

2 Comments

  1. an altermate way in java :




    package duplicateremoval;

    import java.util.Arrays;
    import java.util.Scanner;
    public class removeDuplicate
    {
    static void duplicate(int arr[])
    {

    Arrays.sort(arr);
    }

    public static void main(String[] args)
    {
    int i,size,j=0;
    Scanner sc=new Scanner(System.in);

    System.out.println("Enter the size of the array:");
    size=sc.nextInt();

    int arr[ ]=new int[size];

    System.out.println("Enter the Element of the array:");
    for(j=0;j<size;j++)
    {
    arr[j]=sc.nextInt();
    //Arrays.sort(arr);
    }

    duplicate(arr);
    //Remove duplicate element
    j=0;
    for(i=0;i<size-1;i++)
    {
    if(arr[i]!=arr[i+1])
    {
    arr[j++]=arr[i];

    }
    }
    arr[j++]=arr[size-1];

    System.out.print("After removing duplicate element array is ");

    for(i=0;i<j;i++)
    System.out.print(arr[i]+" ");

    sc.close();
    }

    }

    ReplyDelete

Please do not Enter any spam link in the comment box