Removing Duplicates Array Java

Removing Duplicates Array Java

Summary: Java is so developed by Designers to write code easily for Developers. Forget the laborious days of C/C++ coding. This tutorial "Removing Duplicates Array Java" is the place to prove it.

It is a general and interesting code for beginners and this can be achieved in many ways in Java. This code, I would like to keep as simple as possible using simple Java builtin methods and without using time taking and mind blowing iteration loops.

Concepts used in Removing Duplicates Array Java
  1. Array accepts duplicate elements.
  2. List accepts duplicate elements.
  3. HashSet accepts only unique elements.
  4. TreeSet maintains sorted order.

These concepts are used in the following code for removing duplicates array Java

import java.util.*;
public class NoDuplicates
{
  public static void main(String args[])
  {
    String[] cities1 = { "Hyderabad", "Mumbai", "Chennai", "Mumbai", "Hyderabad" };
    System.out.println("Array with duplicate elements: " + Arrays.toString(cities1));

    List al1 = Arrays.asList(cities1);         // converting array into List
    HashSet s1 = new HashSet(al1);     // passing list to HashSet
                                                       // converting HashSet to array back, if required
    String[] cities2 = new String[s1.size()];
    s1.toArray(cities2);                               // populating array with HashSet elements
    System.out.println("Array with unique elements: " + Arrays.toString(cities2));

                                                       // you can remove and at the same time sort with TreeSet (eliminating HashSet)
    TreeSet t1 = new TreeSet(al1);     // passing list to HashSet
                                                       // converting HashSet to array back, if required
    String[] cities3 = new String[t1.size()];                     
    t1.toArray(cities3);				// populating array with TreeSet elements
    System.out.println("Array unique elements and sorted: " + Arrays.toString(cities3));
  }
}

Removing Duplicates Array Java

The array elements are printed with toString() method of Arrays class. class java.uitl.Arrays comes with many time saving static methods to avoid the loborious code of C/C++ to manipulate array elements.

In the above code, TreeSet can be eliminated using Arrays sort() method.

String[] cities1 = { "Hyderabad", "Mumbai", "Chennai", "Mumbai", "Hyderabad" };
 
HashSet s1 = new HashSet(Arrays.asList(cities1)); // passing list to HashSet
                                                                  // converting HashSet to array back, if required
String[] cities2 = new String[s1.size()];
s1.toArray(cities2);                                              // populating array with HashSet elements
                                                                  // HashSet is used to just to eliminate duplicates
Arrays.sort(cities2);                                             // observe, TreeSet is not used
System.out.println("Array without duplicates and sorted: " + Arrays.toString(cities2));

Lots of simple code is shown here in this tutorial "Removing Duplicates Array Java" and do not stop here. Practice small codes in your leisure to get command over the subject to be noticed in your team.

Leave a Comment

Your email address will not be published.