7: Find the frequency of an element in an array.
⏳⌚ 00:00:00

Question:-

Given an array arr[] of size n, and a target x, Find the frequency of an element in an array.
Example 1:
Input:
N=8, x = 2
arr[] = {1, 2, 9, 6, 3, 2, 5, 2}
Output: 3
Example 2:
Input:
N-6, x =12
arr[] = {90, 80, 100, 70, 40, 30}
Output: 0

Steps to solve:-

Find the frequency of an element in an array. Follow these steps
1. Input:- The code first takes an integer n as input, representing the number of elements in the array. and a target element x, int this problem we have to find the frequency of the x in the array.
2. Array input:- It then takes input for an array of integers arr with n elements using a for loop.
3. Array Traversal:- Another for loop is used to iterate through the elements of the arr array to count the frequency of x.
4. Comparison:- if(arr[i] == x): Checks if the current element of the array is equal to the value of x.
5. Increament frequency:- If the condition in the previous step is true, frequency is incremented by 1. After both for loops have finished running, the program has counted the frequency of x in the array.
6. Output:- Finally, the frequency of x is printed to the standard output .

solution->

View Code 1
Time Complexity = O(n)
Space Complexity = O(n)