- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Golang program to find the frequency of an element in an array
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
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.
- Related Articles
- Write a Golang program to find the frequency of each element in an array
- Write a Golang program to search an element in an array
- Write a Golang program to find the element with the minimum value in an array
- Write a Golang program to search an element in a sorted array
- Golang Program to Find the Largest Element in an Array
- Write a Golang program to reverse an array
- Golang Program To Append An Element Into An Array
- Golang Program To Remove All Occurrences Of An Element In An Array
- Write a program to find the index of particular element in an array in javascript?
- Golang program to fill an array with a specific element
- Write a Golang program to find pairs with the given sum in an array(O(n2))
- Write a Golang program to find pairs with the given sum in an array(O(n))
- Golang Program to add an element in the array at the beginning
- Write a Golang program to find pairs with given sum in an array(O(nlogn))
- Write a Golang program to sort an array using Bubble Sort

Advertisements