Problem overview
The function count receives an IntArray named nums and an Int named value. It returns how many elements of nums are equal to value. The statement asks you to write the loop yourself instead of calling a built-in counting function.
In the example, nums = [1, 4, 2, 2, 5, 2] and value = 2. The number 2 sits at three positions, so the function returns 3.
Approach: look at every element once
An array has no shortcut for this question. To know how many elements equal value, you have to examine each one. So the plan is simple: keep a counter that starts at zero, walk through the array, and add one each time an element matches.
The array is not sorted, and the elements can repeat in any order, so a match can be anywhere. Each element is examined once, so the time grows in step with the array length. Only the counter is stored in addition to the input, so the extra space is constant.
Step by step
- Declare the counter.
var answer = 0holds the count so far. It must be avar, since avalcan't change after it is set. - Loop over the elements with
for (num in nums). Each pass gives you the next element innum, so every element is visited exactly once, in order, and you don't need an index. - Compare and count. Inside the loop, an
ifchecks whethernum == value. Only on a match does the code runanswer++. Elements that differ are skipped, and the loop moves on. - Return after the loop. The total is known only when all elements have been checked. A
returninside the loop would end the function after the first element.
The solution in Kotlin
class Solution {
fun count(nums: IntArray, value: Int): Int {
var answer = 0
for (num in nums) {
if (num == value) {
answer++
}
}
return answer
}
}
The Kotlin details behind it:
==compares values. In Kotlin,==callsequals, so for numbers it asks whether they are equal. The reference-identity operator is===, which you don't need here. Writing a single=inside the condition does not compile, because an assignment is not aBooleanexpression.for (num in nums). The loop variable is declared by the loop and is read-only inside the body. If you need positions as well as values, loop overnums.indicesor usewithIndex()instead.answer++. It adds 1 to avar.answer += 1means the same.IntArray. It is Kotlin's array of primitiveIntvalues, so the elements are compared directly and the array can't containnull.- Built-ins exist. Kotlin's standard library offers
nums.count { it == value }. The lesson asks you not to use it, because the point is to practise the loop.
Complexity
Let n be the length of nums.
Time: O(n). The loop does one comparison per element, and no more than one increment per element.
Space: O(1). Only answer and the loop variable are stored, and neither depends on n.
Edge cases and common mistakes
- The value is absent. The counter never moves, and the function returns 0.
- An empty array. The loop body never runs, so the result is 0.
- Every element matches.
intArrayOf(3, 3, 3)withvalue = 3returns 3, the array length. - Negative values.
intArrayOf(-1, 1, -1)withvalue = -1returns 2. Nothing special is needed. - Returning inside the loop. The function would stop after the first element and return 0 or 1.
- Resetting the counter. Declaring
answerinside the loop creates a fresh zero on every pass, so the count never builds up. - Incrementing without the check. If
answer++sits outside theif, the function returns the array length.
Testing
nums = [1, 4, 2, 2, 5, 2],value = 2gives3.nums = [1, 4, 2],value = 7gives0.nums = [],value = 7gives0.nums = [3, 3, 3],value = 3gives3.nums = [-1, 1, -1],value = -1gives2.