Start learning free

LESSON 141 OF 338 · LOOPS & GLOBAL VARIABLES

Number of Occurrences in an Array in Go

Practice Read the full explanation

Given an array of integers nums, count and return the number of occurrences of a given value.

Example:

Input: nums = [1, 4, 2, 2, 5, 2], value = 2
Output: 3
Explanation: the value 2 appears 3 times in the array

Note:

Do not use builtin functions, it would defy the purpose of the challenge. Write the whole code yourself.

STEP 1 OF 4
    Ctrl + Enter to check

    GO · LOOPS & GLOBAL VARIABLES

    Array Count in Go, explained

    Problem overview

    The function count receives a slice of integers, nums, and an integer value. It returns how many times value appears in nums. The task asks you to write the loop yourself instead of calling a ready-made function.

    For nums = [1, 4, 2, 2, 5, 2] and value = 2, the 2 sits at three positions, so the answer is 3.

    Approach

    This is the basic counting pattern. Keep a counter that starts at 0, look at every element once, and add 1 to the counter each time the element equals value. When the loop ends, the counter holds the answer.

    Every element has to be inspected, because any of them might match. Unlike a search for the first match, there is nothing to stop early for.

    Tracing the example

    With nums = [1, 4, 2, 2, 5, 2] and value = 2, the counter changes like this as the loop moves through the slice:

    1. 1 is not 2, so answer stays 0.
    2. 4 is not 2, so it stays 0.
    3. 2 matches, so answer becomes 1.
    4. The next 2 matches, so answer becomes 2.
    5. 5 does not match, so it stays 2.
    6. The last 2 matches, so answer becomes 3.

    The loop ends and the function returns 3. The same pattern, a counter plus a condition inside a loop, answers many similar questions: how many even numbers there are, how many grades are below a limit, how many characters are vowels. Only the condition changes.

    Step by step

    1. Create the counter. answer := 0 declares an int and infers its type from the literal. It must be declared before the loop, otherwise it would be reset on every pass.
    2. Loop over the slice with for ... range. Each iteration produces an index and an element. Only the element matters, and Go rejects declared-but-unused variables, so the index is discarded with _.
    3. Compare and count. Inside the loop, an if checks whether num == value and increments answer on a match. Elements that differ are skipped.
    4. Return after the loop. A return inside the loop would end the function after the first element.

    The solution in Go

    func count(nums []int, value int) int {
        answer := 0
        for _, num := range nums {
            if num == value {
                answer++
            }
        }
        return answer
    }
    

    The Go details worth knowing:

    • ++ is a statement. In Go, answer++ is a complete statement on its own line, not an expression. Code such as x := answer++ or return answer++ does not compile.
    • No parentheses around conditions. Go writes if num == value { with no parentheses, and the braces are required even for a one-line body.
    • Elements are copies. num is a copy of the current element. That is fine here because the function only reads the values. Changing num would not change the slice.
    • The type is []int, a slice. The lesson calls it an array, but a slice is what Go code normally passes around. range and len work the same on both.

    Complexity

    Time: O(n). The loop makes one pass, with a constant amount of work per element.

    Space: O(1). The function keeps one counter and the loop variable.

    Edge cases and common mistakes

    • Value not present. The counter never changes, so the function returns 0.
    • Empty or nil slice. The loop body runs zero times and the function returns 0. Ranging over a nil slice is safe in Go.
    • Every element matches. For []int{3, 3, 3} and 3, the result is 3.
    • Declaring the counter inside the loop. It would restart at 0 on every pass, and the code would not compile anyway, because answer would not exist at the return.
    • Returning inside the loop. The function would stop after the first element.
    • Using the index as the count. The index tells you where an element is, not how many matches have been seen.

    Testing

    • []int{1, 4, 2, 2, 5, 2} with 2 gives 3.
    • []int{1, 4, 2, 2, 5, 2} with 7 gives 0.
    • []int{3, 3, 3} with 3 gives 3.
    • []int{} with 1 gives 0.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free