Problem overview
The function contains receives an IntArray named nums and an Int named value. It returns true if value appears anywhere in nums and false if it doesn't. The task forbids ready-made search helpers, so the loop is yours to write.
Two examples show the behaviour. In [1, 2, 4, 5] the value 4 is present, so the answer is true. In [-1, 2, -4, 0, 10] the value 7 never appears, so the answer is false.
Approach: look at every element until you find a match
An unsorted array gives no hint about where a value might be, so the only reliable method is to inspect the elements one by one. This is a linear search. Two facts shape the code:
- A single match is enough to answer
true. There is no reason to keep looking, so the function can return immediately. - The answer can be
falseonly after every element has been compared. That means thefalsebelongs after the loop, not inside it.
The common wrong turn is to answer false inside the loop as soon as one element differs from value. One mismatch says nothing about the elements that come later.
Step by step
- Loop over the array. A
forloop with a variable callednumvisits each element in order, so every element is compared withvalue. No index is needed because only the values matter. - Compare inside the loop. If
numequalsvalue, returntrue. Areturnends the whole function at once, so the elements after the match are never examined. - Return
falseafter the loop. The only way to reach that line is to have gone through the entire array without a match, so noelseis required.
The solution in Kotlin
class Solution {
fun contains(nums: IntArray, value: Int): Boolean {
for (num in nums) {
if (num == value) {
return true
}
}
return false
}
}
Kotlin details worth knowing:
for (num in nums). The loop variablenumis a read-only value that takes each element in turn. Arrays can be looped over directly, with no.lengthor index arithmetic.==compares values. For numbers,num == valuechecks that they are equal. For objects such as strings, Kotlin's==callsequalsand compares contents, and===is the operator that compares references. PlainIntcomparisons need no care.- The return type must always be reached. The function is declared to return a
Boolean, and the compiler checks that every path ends in areturn. If the finalreturn falseis missing, the code is rejected withmissing return statement, even though the loop could return. - Ready-made helpers exist. Kotlin has a built-in
contains(theinoperator) for arrays and collections. The exercise asks you to write the loop yourself so you learn what that helper does.
Complexity
Time: O(n), where n is the number of elements. In the worst case the value is last or absent, and every element is compared once. If the value is the first element, the function returns after one comparison.
Space: O(1). The function stores only the loop variable.
Edge cases and common mistakes
- Empty array. The loop body never runs and the function returns
false, which is right: nothing can be found in an empty array. - Returning
falsetoo early. Puttingelse { return false }inside the loop compares only the first element. For[1, 2, 4, 5]and4it would answerfalseafter seeing1. - Duplicates. The first match ends the search, so repeated values cost nothing extra.
- Negative numbers and zero. They are ordinary values.
[-1, 2, -4, 0, 10]contains0and-4, and the comparison treats them like any other number. - The value at the end. A match at the last position is found only by the final pass of the loop. Stopping one element early is an easy off-by-one error when you manage the index by hand.
Testing
nums = [1, 2, 4, 5],value = 4givestrue.nums = [-1, 2, -4, 0, 10],value = 7givesfalse.nums = [],value = 7givesfalse.nums = [-1, 2, -4, 0, 10],value = 10givestrue.