How about using a HashSet and comparing a Hashset size with the length of the original array?
HashSet gets rid of duplicates, so if the size is the same as the length of the array, this will mean that all elements of the array are different.
Example:
import java.util.Arrays; import java.util.HashSet; public class QuickTester { public static void main(String[] args) { String[] hands = new String[]{"Zilch", "Pair", "Triple", "Straight", "Full House"}; HashSet<String> hs = new HashSet<>(Arrays.asList(hands)); if(hs.size() == hands.length) { System.out.println("All elements in array are different!"); } else { System.out.println("Duplicates found in array!"); } hands = new String[]{"Banana", "Apple", "Orange", "Banana"}; hs = new HashSet<>(Arrays.asList(hands)); if(hs.size() == hands.length) { System.out.println("All elements in array are different!"); } else { System.out.println("Duplicates found in array!"); } } }
Output:
All elements in array are different! Duplicates found in array!
Gosu
source share