Well, if you need the distinct elements and not just the number, you may use a Set.
A Set is:
A collection that contains no duplicate elements.
You keep adding your elements to the set, and then just look at what the set contains.
Something similar to this:
public static String[] getDistinct(String[] input) {
Set<String> distinct = new HashSet<String>();
for(String element : input) {
distinct.add(element);
}
return distinct.toArray(new String[0]);
}
Usage:
String[] input = new String[] {"a", "b", "a", "c", "a", "b", "d"};
String[] distinct = getDistinct(input);
for(String element : distinct) {
System.out.println(element);
}
Result:
d b c a
Note that the order of the elements may not be preserved.
To find the number of the distinct elements, use:
getDistinct(input).length