Problem overview
The method count receives an array of integers nums and an integer value. It returns how many times value appears in nums. For nums = Array(1, 4, 2, 2, 5, 2) and value = 2, the 2 appears at three positions, so the answer is 3.
The note asks you to write the loop yourself, not to call a ready-made counting function from the library. The point is to practise scanning an array.
Approach: scan once and keep a tally
You can't know the answer without looking at every element, because any of them might match. So the method walks the array from the first element to the last. It keeps a counter that starts at 0, and each time the current element equals value, the counter goes up by one. When the walk ends, the counter is the answer.
Equal elements are counted separately, so duplicates cause no problem. The array doesn't need sorting.
Step by step
- Declare
var answer = 0. It must be avar, since avalcan't be reassigned and the counter changes. Scala infersInt. - Loop with
for (num <- nums). The arrow<-is a generator: the body runs once per element, in order, withnumholding the current one. - Compare with
if (num == value)and add one withanswer += 1on a match. Scala has no++, so+= 1is the way to increment. The==operator compares values, which forIntis what you want. - Return
answerafter the loop. Returning inside the loop would end the method after the first element.
The solution in Scala
class Solution {
def count(nums: Array[Int], value: Int): Int = {
var answer = 0
for (num <- nums) {
if (num == value) {
answer += 1
}
}
return answer
}
}
Tracing the example with value = 2:
num | Equals 2? | answer afterwards |
|---|---|---|
| 1 | no | 0 |
| 4 | no | 0 |
| 2 | yes | 1 |
| 2 | yes | 2 |
| 5 | no | 2 |
| 2 | yes | 3 |
Language details
- The loop variable
numis a freshvalon each pass. You read it but can't assign to it, and changing the array isn't needed here. - The loop variable
numand the parametervalueare two different names. Keeping them distinct makes the comparison easy to read. - Because the
ifhas noelse, nothing happens for elements that don't match.
Complexity
The loop visits each of the n elements once and does a constant amount of work for each, so the time is O(n). Only answer and the loop variable are stored, so the extra space is O(1).
Edge cases and common mistakes
- The value is absent. No element matches, so the method returns 0.
- An empty array. The loop body never runs and the method returns 0.
- Every element matches. The method returns the array's length.
- Returning inside the loop. The method would stop after the first element and report at most 1.
- Using
=instead of==. Writingif (num = value)is an attempt to assign to the loop variable, and the compiler stops with the errorReassignment to val num. - Declaring
answerinside the loop. It would reset to 0 on every pass and the final count would be 0 or 1.
Testing
Try Array(1, 4, 2, 2, 5, 2) with value 2 (3), Array(1, 2) with value 7 (0), Array(7, 7, 7) with value 7 (3) and an empty array (0).
