Introduction
Finding the frequency of elements in an array involves counting how many times each element appears in the array. This guide will demonstrate how to write a Go program that counts and displays the frequency of each element in an array.
Problem Statement
Create a Go program that:
- Prompts the user to enter the number of elements in an array.
- Takes input for the elements of the array.
- Finds and displays the frequency of each element in the array.
Example:
- Input: Array elements: [1, 2, 2, 3, 4, 4, 4]
- Output:
Element 1 occurs 1 times Element 2 occurs 2 times Element 3 occurs 1 times Element 4 occurs 3 times
Solution Steps
- Import the fmt Package: Use import "fmt"to include the fmt package for formatted I/O operations.
- Write the Main Function: Define the mainfunction, which is the entry point of every Go program.
- Input the Number of Elements: Use fmt.Scanlnto take input from the user for the number of elements in the array.
- Input the Array Elements: Use a loop to input the elements of the array from the user.
- Find the Frequency of Elements: Use a map to count the frequency of each element in the array.
- Display the Frequency of Each Element: Use a loop to print the frequency of each element.
Go Program
package main
import "fmt"
/**
 * Go Program to Find the Frequency of Elements in an Array
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the number of elements in the array
    var n int
    // Step 2: Prompt the user to enter the number of elements
    fmt.Print("Enter the number of elements in the array: ")
    fmt.Scanln(&n)
    // Step 3: Declare an array to hold the elements
    array := make([]int, n)
    // Step 4: Input the elements of the array
    fmt.Println("Enter the elements of the array:")
    for i := 0; i < n; i++ {
        fmt.Scanln(&array[i])
    }
    // Step 5: Find the frequency of each element using a map
    frequencyMap := make(map[int]int)
    for _, value := range array {
        frequencyMap[value]++
    }
    // Step 6: Display the frequency of each element
    fmt.Println("Frequency of each element:")
    for key, value := range frequencyMap {
        fmt.Printf("Element %d occurs %d times\n", key, value)
    }
}
Explanation
Step 1: Declare Variables
- The variable nis declared to store the number of elements in the array.
Step 2: Input the Number of Elements
- The program prompts the user to enter the number of elements using fmt.Print. Thefmt.Scanlnfunction reads the input and stores it in thenvariable.
Step 3: Declare the Array
- An array arrayis created usingmaketo holdninteger elements.
Step 4: Input the Array Elements
- The program uses a forloop to prompt the user to enter each element of the array, storing the input in the corresponding index ofarray.
Step 5: Find the Frequency of Each Element
- A map frequencyMapis used to track the frequency of each element in the array. The program iterates over the array and increments the count for each element in the map.
Step 6: Display the Frequency of Each Element
- The program iterates over frequencyMapand prints the frequency of each element usingfmt.Printf.
Output Example
Example 1:
Enter the number of elements in the array: 7
Enter the elements of the array:
1
2
2
3
4
4
4
Frequency of each element:
Element 1 occurs 1 times
Element 2 occurs 2 times
Element 3 occurs 1 times
Element 4 occurs 3 times
Example 2:
Enter the number of elements in the array: 5
Enter the elements of the array:
10
20
10
30
20
Frequency of each element:
Element 10 occurs 2 times
Element 20 occurs 2 times
Element 30 occurs 1 times
Conclusion
This Go program demonstrates how to find the frequency of elements in an array using a map to count occurrences. It covers basic programming concepts such as arrays, loops, and map data structures. This example is useful for beginners learning Go programming and understanding how to analyze the frequency of elements within a collection.