Java method to check if an array has duplicate elements

  • 2020-04-01 04:01:58
  • OfStack

This article illustrates a Java method for checking whether an array has duplicate elements. Share with you for your reference. The specific implementation method is as follows:


//Determines if there are duplicate values in the array
public static boolean checkRepeat(String[] array){
  Set<String> set = new HashSet<String>();
  for(String str : array){
    set.add(str);
  }
  if(set.size() != array.length){
    return false;//There are repeated
  }else{
    return true;//Don't repeat
  }
}

I hope this article has been helpful to your Java programming.


Related articles: