1
public class FindNumber {

            static String findNumber(List<Integer> arr, int k) {
                String res = "YES";
    //Unable to identify problem with this part of the code
                for (int i = 0; i < arr.size(); i++) {
                    if (k == arr.get(i))
                        res = "YES";
                    else
                        res = "NO";

                }

                return res;

            }
}

Above code returns NO as the answer even if the integer is present in the list.

5
  • 3
    Using == to compare Integer objects only works if the values are between -128 and 127, all other values will need to be compared using the .equals() method instead Commented Jul 18, 2019 at 9:20
  • there are several things wrong with your code. If you want true or false, return a boolean (for instance) Commented Jul 18, 2019 at 9:24
  • @JonK there is no comparison between Integer objects. The comparison is between Integer object and int primitive. Integer object is auto unboxed in this case. Commented Jul 18, 2019 at 9:25
  • @THe_strOX Absolutely correct, and that's why it's a comment instead of an answer. It's quite an easy gotcha to get bitten by when you're first starting out, so I feel it's still worth mentioning. Commented Jul 18, 2019 at 9:28
  • @JonK That makes sense. Commented Jul 18, 2019 at 9:29

6 Answers 6

1

You could just use arr.contains() to get a boolean value of whether Integer is on the list or not. An then you can translate this value to YES or NO (if you do really need it):

String yesNo = arr.contains(k) ? "YES" : "NO";
Sign up to request clarification or add additional context in comments.

1 Comment

or, change the returntype to boolean and say: return arr.contains(k);
0

This will work:

static String findNumber(List<Integer> arr, int k) {
            String res = "YES";
            for (int i = 0; i < arr.size(); i++) {
                if (k == arr.get(i))
                    res = "YES";
                    break;
                else
                    res = "NO";

            }

            return res;

        }

Once you find the integer you have to stop the loop, and you can do this using a break

1 Comment

if you want to go for a fix like that, you should have just replaced the whole if else with if ( found ) return "YES";
0

try optimize your code....

way 1 (using for-each loop):

 static String findNumber(List<Integer> arr, int k) { 
        for (Integer integer : arr) {
            if (integer == k) {
                return "YES";
            }
        }
        return "NO"; 
    }

another way is (using ternary operator) :

static String findNumber(List<Integer> arr, int k) { 
    return arr.contains(k) ? "YES" : "NO";
}

Comments

0

Using streams:

static String findNumber(List<Integer> arr, int k) {
    return arr.stream()
        .filter(e -> e == k)
        .findFirst()
        .map(e -> "YES")
        .orElse("NO");
}

Comments

0
    public static String isListContainsNumber(List<Integer> nums, int n) {
           return nums.stream().anyMatch(el -> el.equals(n)) ? "YES" : "NO";
    }

Comments

0

The main problem with your code is that even if it finds an integer object in the ArrayList, after setting res = Yes, it still continues the iteration. Therefore there is a possibility that there might be other values inside the list of not the desired data type, thereby setting res back to No. The solution here is to use a jump statement such as break which would terminate the loop process as soon as an integer is encountered. Hope it helps!

1 Comment

Thanks :) .. jump statement is something i missed. Adding "break" after return "YES" solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.