RedYoel
RedYoel

Reputation: 313

How to Check If Two String Arrays are The Same

I'm trying to solve a problem on "practice it", and I'm not able to pass all the tests to it.

The problem is:

Write a method called equals that takes in two string arrays and returns true if they are equal; that is, if both arrays have the same length and contain equivalent string values at each index.

I have tried the following code, but the test to the input equals({"a", "b", "a", "c", "a", "d", "a", "e", "a"}, {"x", "b", "a", "c", "a", "d", "a", "e", "a"}) but it doesn't work.

public static boolean equals (String [] txt1, String [] txt2){
    boolean result=false;
    if(txt1.length==txt2.length){
        for(int i=0; i<txt1.length; i++){
            if(txt1[i].equals(txt2[i])){
                result = true;
            }
            else {
                result = false;
            }
        }
    }
    else {
        return false;
    }
    return result;
}

Expected return: false My return: true

Upvotes: 2

Views: 2244

Answers (3)

viper-zero
viper-zero

Reputation: 41

After the for loop you should check if the arrays aren't equal and if so return false. If the program doesn't return false, return true instead.

public static boolean equals(String[] txt1, String[] txt2) {
    if (txt1.length == txt2.length) {
        for(int i = 0; i < txt1.length; i++) {
            if(!txt1[i].equals(txt2[i])) {
               return true;
            }
        }
    } else {
        return false;
    }
    return true;
}

Upvotes: 0

Michal Horvath
Michal Horvath

Reputation: 183

See explanation in comments:

public static boolean equals(String[] txt1, String[] txt2) {
    if (txt1.length == txt2.length) {
        for (int i = 0; i < txt1.length; i++) {
            if (txt1[i].equals(txt2[i])) {
                // do nothing
            } else {
                // at this moment you know that arrays are different
                // so you can return false without checking the rest 
                // of the array
                return false;
            }
        }
        // here you checked all array, you know that each element is
        // same, because if it wouldn't, it would return false already
        // so you can return true now
        return true;
    } else {
        return false;
    }
}

Some improvements can be made to your code, again read comments:

public static boolean equals(String[] txt1, String[] txt2) {
    if (txt1 == txt2) {
        // booth are null or booth are same instance
        return true;
    }
    if(txt1 == null || txt2 == null) {
        // one of the arrays is null so code bellow yould fail
        return false;
    }
    if (txt1.length == txt2.length) {
        for (int i = 0; i < txt1.length; i++) {
            if (!txt1[i].equals(txt2[i])){
                // equal rewriten to not equal
                return false;
            }
        }
        return true;
    }
    // no need to write else since every branch in if will result in return
    return false;
}

Upvotes: 2

UnholySheep
UnholySheep

Reputation: 4096

The problem lies in the loop:

for(int i=0; i<txt1.length; i++){
    if(txt1[i].equals(txt2[i])){
         result = true;
    }
    else {
         result = false;
    }
}

The if gets executed for every single element, so in essence your code only checks if the last element is the same for both arrays, as it overrides previous result = false; occurrences.

The correct solution is to immediately stop and return false once a single element is different:

for(int i=0; i<txt1.length; i++){
    if(txt1[i].equals(txt2[i])){
         result = true;
    }
    else {
         return false;
    }
}

Upvotes: 3

Related Questions