Program to remove duplicate elements in an array

Program to remove duplicate elements in an array

10 May 2023

10 May 2023

Write Program to remove duplicate elements in an array



Description about program to remove duplicate elements in an array

Get an array as input from the user and then remove all the duplicate elements in that array.

Input

Enter the size of array

5

Enter the elements of array

35  35  45  60  60

Output

35 45 60

 

C Program to remove duplicate elements in an array

#include<stdio.h>

int dupRemove(int arr[], int n)

{

   if (n==0 || n==1)

     return n;

   int temp[n];

   int j = 0;

   for(int i=0; i<n-1; i++)

   {

      if (arr[i] != arr[i+1])

         temp[j++] = arr[i];

   }

   temp[j++] = arr[n-1];

   for(int i=0; i<j; i++)

     arr[i] = temp[i];

   return j;

}

int main()

{

    int n;

    printf("Enter the size of array: ");

    scanf("%d",&n);

    int arr[n];

    printf("Enter the elements of array: ");

    for(int i=0;i<n;i++)

    scanf("%d",&arr[i]);

    int s = dupRemove(arr, n);

   for (int i=0; i<s; i++)

      printf("%d ", arr[i]);

 

   return 0;

}

 

C++ Program to remove duplicate elements in an array

#include<iostream>

using namespace std;

int dupRemove(int arr[], int n)

{

   if (n==0 || n==1)

     return n;

   int temp[n];

   int j = 0;

   for(int i=0; i<n-1; i++)

   {

      if (arr[i] != arr[i+1])

         temp[j++] = arr[i];

   }

   temp[j++] = arr[n-1];

   for(int i=0; i<j; i++)

     arr[i] = temp[i];

   return j;

}

int main()

{

    int n;

    cout<<"Enter the size of array: ";

    cin>>n;

    int arr[n];

    cout<<"Enter the elements of array: ";

    for(int i=0;i<n;i++)

    cin>>arr[i];

    int s = dupRemove(arr, n);

   for (int i=0; i<s; i++)

      cout<<arr[i]<<" ";

 

   return 0;

}

 

Java Program to remove duplicate elements in an array

import java.util.Scanner;

class Main

{

   static int removeDup(int arr[], int n)

   {

      if (n==0 || n==1)

         return n;

      int j = 0;

      for (int i=0; i<n-1; i++)

          if (arr[i] != arr[i+1])

             arr[j++] = arr[i];

      arr[j++] = arr[n-1];

      return j;

   }

   public static void main (String[] args)

   {

    int n;

    Scanner sc = new Scanner(System.in);

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

    n = sc.nextInt();

    int[]arr = new int[n];

    System.out.println("Enter the array elements: ");

    for(int i = 0; i < n; i++)

    {

        arr[i] = sc.nextInt();

    }

      int s = removeDup(arr, n);

      for (int i=0; i<s; i++)

        System.out.print(arr[i]+" ");

   }

}

 

Python Program to remove duplicate elements in an array

def removeDup(arr, n):

    if n == 0 or n == 1:

        return n

    temp = list(range(n))

    j = 0;

    for i in range(0, n-1):

        if arr[i] != arr[i+1]:

            temp[j] = arr[i]

            j += 1

 

    temp[j] = arr[n-1]

    j += 1

 

    for i in range(0, j):

        arr[i] = temp[i]

    return j

 

n = int(input("Enter size of array: "))

arr = []

print("Enter array elements: ")

for i in range(0,n):

                temp = int(input())

                arr.append(temp)

n = removeDup(arr, n)

for i in range(n):

    print ("%d"%(arr[i]), end = " ")


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023


Related Articles

Ask Us Anything !