Java array remove duplicate elements

Description

Java array remove duplicate elements



import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Main {
  public static void main(String[] args) {
    String[] s = {"CSS", "HTML", "CSS","Java","Java", "Javascript",
        "CSS","SQL","CSS"};
    System.out.println(Arrays.toString(s));
    //from   w ww .j  a v a  2s.co  m
    s = clean(s);
    
    System.out.println(Arrays.toString(s));
  }

  public static String[] clean(String[] input) {
    Set<String> unique = new HashSet<>(Arrays.asList(input));
    return unique.toArray(new String[unique.size()]);
  }
}



PreviousNext

Related