Java program to remove duplicates in an array

    Array is a collection of similar data types. But an element in the array may be repeated. Let us remove the duplicate elements in the array.

It has two types of implementation.

  1. ·       Using Hash Set method.
  2. ·       Using stream method.

Each method is coated with an example.

1.Using Hash Set method:

              This method used “Hash Set”. The steps to implement the method is given below.

  • ·       First, include the built in packages java.util.Arrays and java.util.HashSet.
  • ·       Create a class with main() function.
  • ·       A integer array  a is declared and assigned with elements.
  • ·       The original array is printed.
  • ·       First, the hash set object is developed.
  • ·       Using a for loop, hash set gets assigned with array values.
  • ·       A new array is created with the hash set size.
  • ·       Each element in the array and hash set is checked. If any duplicate occurs, it is deleted.
  • ·       Finally, the array with no duplicate is displayed.

Program:

import java.util.Arrays;

import java.util.HashSet;

public class RemoveDuplicatesEg {

    public static void main(String[] args) {

        int[] a = {110, 220, 220, 83, 40, 110, 50, 83};

         System.out.println("The original elements are :" + Arrays.toString(a));

        // let us remove duplicates by Converting array to HashSet

        HashSet<Integer> set1 = new HashSet<>();

        for (int no : a) {

            set1.add(no);

        }

        // Let us retain the hashset to array

        int[] uArray = new int[set1.size()];

        int index = 0;

        for (int no : set1) {

            uArray[index++] = no;

        }

        System.out.println("The elements of Array without duplicates: " + Arrays.toString(uArray));

    }

}

Output:

C:\raji\blog>javac RemoveDuplicatesEg.java

C:\raji\blog>java RemoveDuplicatesEg

The original elements are :[110, 220, 220, 83, 40, 110, 50, 83]

The elements of Array without duplicates: [50, 83, 40, 220, 110]

2. Using stream method:

              This method uses the built-in methods “stream(), distinct(), toArray()” to remove duplicates in the array.

Program:

import java.util.Arrays;

import java.util.stream.IntStream;

public class RemoveDuplicatesStreamEg {

    public static void main(String[] args) {

        int[] a = {110, 23, 39, 23, 40, 110, 50, 39};

        System.out.println("The original elements are :" + Arrays.toString(a));

        int[] uArray = Arrays.stream(a)

                .distinct()

                .toArray();

        System.out.println("The elements in the array without duplicates: " + Arrays.toString(uArray));

    }

}

Output:

C:\raji\blog>javac RemoveDuplicatesStreamEg.java

C:\raji\blog>java RemoveDuplicatesStreamEg

The original elements are :[110, 23, 39, 23, 40, 110, 50, 39]

The elements in the array without duplicates: [110, 23, 39, 40, 50]

These are the ways to create the java program to remove the duplicates in the array. Hope these codes are useful to you. Keep coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.