Find the duplicate items in an array in Java
Description
The following code shows how to find the duplicate items in an array.
Example
/* ww w . j a v a 2 s .c om*/
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> s = new HashSet<String>();
String[] data = new String[]{"java2s.com","a","v","c","d","java2s.com",};
for (String a : data)
if (!s.add(a))
System.out.println("Duplicate detected: " + a);
System.out.println(s.size() + " distinct words: " + s);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »