# Write a Golang program to find the frequency of an element in an array

Go ProgrammingServer Side ProgrammingProgramming

#### Cyber Security : Go from Zero to Hero

Most Popular

60 Lectures 5.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

## Examples

In the Input array, arr = [2, 4, 6, 7, 8, 1, 2]

Frequency of 2 in given array is 2

Frequency of 7 is 1

Frequency of 3 is 0.

## Approach to solve this problem

Step 1: Define a function that accepts an array and a num

Step 2: Declare a variable count = 0.

Step 3: Iterate the given array and increase the count by 1 if num occurs in the array.

Step 4: Print count for the given num.

## Program

Live Demo

package main
import "fmt"
func findFrequency(arr []int, num int){
count := 0
for _, item := range arr{
if item == num{
count++
}
}
fmt.Printf("Frequency of %d in given array is %d.\n", num, count)
}

func main(){
findFrequency([]int{2, 4, 5, 6, 3, 2, 1}, 2)
findFrequency([]int{0, 1, 3, 1, 6, 2, 1}, 1)
findFrequency([]int{1, 2, 3, 4, 5, 6, 7}, 10)
}

## Output

Frequency of 2 in given array is 2.
Frequency of 1 in given array is 3.
Frequency of 10 in given array is 0.