Elgin Beloy
Elgin Beloy

Reputation: 93

How to test if a String equals any other Strings in an Array in java

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.

Upvotes: 1

Views: 3374

Answers (5)

Lubo Kanev
Lubo Kanev

Reputation: 480

If you want to check if the strings match backwards too, you could create another set (backwardsSet) with the backward strings and put it in the other set only if it's not present in the backwardsSet.

Upvotes: 1

lub094
lub094

Reputation: 103

Try this:

int strings = 0;
int arrayLength = x.length;

for (int currentNum = 0; currentNum < arrayLength; currentNum++) {
    ++strings;

    for (int i = 0; i < arrayLength; i++) {
        if (i != currentNum && x[currentNum].equals(x[i])) {
           --strings;
           break;      
        }
    }
} 

return strings;

This way you add 1 for the current string, then check all others (not including himself) and if you find you, you subtract 1.

Upvotes: 1

Claude Martin
Claude Martin

Reputation: 765

If you really just want to count distinct strings then simply use this:

Arrays.stream(strings).distinct().count()

And if you want to ignore palindromes:

 final String[] strings = { "ab", "ba", "ab", "cd" };
    // I assume a and b are not null
    final BiPredicate<String, String> equal = 
        (a, b) -> a.equals(b) || new StringBuilder(a).reverse().toString().equals(b);
    final Set<String> set = new HashSet<>();
    for (final String s1 : strings)
      if (!set.stream().anyMatch(s2 -> equal.test(s1, s2)))
        set.add(s1);
    System.out.println(set.size());

If you need to process a lot of strings you could optimize this. Strings of different lengths could not be equal. Yo you could create a partition where you have subsets of strings of same size. Then process those subsets (see Collectors.groupBy(...)).

Upvotes: 0

Paul Boddington
Paul Boddington

Reputation: 37645

There are 2 problems with your code. Firstly, array indices start at 0, not 1. Secondly, the break takes you to the line strings++, when you want to skip that line. Instead you want to continue the outer loop so that the line strings++ is not executed. Annoyingly you cannot use continue on its own as this applies to the inner loop. You can use a label, like this:

loop:
for (int currentNum = 0; currentNum < arrayLength; currentNum++) {
    for (int i = currentNum + 1; i < arrayLength; i++) {
        if (x[currentNum].equals(x[i])) {
            continue loop;
        }
    }
    strings++;
}

Upvotes: 4

Siva Kumar
Siva Kumar

Reputation: 2006

Please try with hashset.

    int strings = 0;
    int arrayLength = x.length;

    HashSet<String> unique = new HashSet<String>(Arrays.asList(x));
    return unique.size();

I have added sample code snippet. Please try this.

Upvotes: 1

Related Questions