Is their any predefined function in Java
such that I can check if all the elements in an array
are different? Or do I have to write a function from scratch to find it?
I have a String array
below:
String[] hands = {"Zilch", "Pair", "Triple", "Straight", "Full House"};
boolean noDupes(Object[] array) {
return Arrays.stream(array).allMatch(new HashSet<>()::add);
}
Stops as soon as it finds a duplicate, rather than going through the entire array and comparing sizes at the end. Conceptually the same as Misha's answer, but using Java 8 functional programming features (streams and method references).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With