count9

Easy · Loops and Iterations · 3 test cases

Given an array of integers, count how many times the number 9 appears in the array.

Examples

count9([1, 2, 9]) → 1
count9([1, 9, 9]) → 2
count9([1, 9, 9, 3, 9]) → 3

Starter code

function count9(nums) {

}

Complexity of the optimal solution

Time O(n), space O(1). The function iterates through the input array once. The time taken is directly proportional to the number of elements (n) in the array. It uses a single counter variable, so the extra space is constant.

Solve count9 in the browser