I have an String array, I want to see how many distinct strings there are in this array. Meaning how many string's there are, not counting repeated strings. Here is what I have:
int strings = 0;
int arrayLength = x.length;
for (int currentNum = 1; currentNum < arrayLength; currentNum++) {
for (int i = currentNum + 1; i < arrayLength; i++) {
if (x[currentNum].equals(x[i])) {
break;
} else {
strings++;
}
}
return strings;
}
But this never returns the correct amount, can anyone tell me why?
UPDATE:
Out of curiosity what If the I also wanted to check if the string was equal to other strings backwards? Then HashSets will not work.
stringsevery time in the outer loop and the inner loop does basically nothing. Not to mention that what you're doing is very unwieldy. A shorter way would be to put all theStringsin aHashSetand check its size.{"A", "A"}?0or1?