Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

True if arr does not contain target

The goal is to return true if arr does not contain target and false otherwise:

public boolean arrayDoesNotContain(int[] arr, int target){
    for (int element : arr) {
        if (element != target) {
            return true;
        } 
    }
    return false;
    
}

I ran this and am getting assertion errors on my tests even though it
fulfills the required task. Any tips or a second pair of eyes to help
me see where this went wrong is appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s difficult to pinpoint the issue without seeing the tests and the specific assertion errors you’re receiving, but one thing to consider is that the current implementation will return true as soon as it finds an element in the array that is not equal to the target, without checking the rest of the elements.

Try this and let me know if that works

public boolean arrayDoesNotContain(int[] arr, int target){
    for (int element : arr) {
        if (element == target) {
            return false;
        } 
    }
    return true;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading