Given an array arr of positive integers and an integer x. Return the frequency of x in the array.
Examples:
Input: arr = [1, 1, 1, 1, 1], x = 1
Output: 5
Explanation: Frequency of 1 is 5.Input: arr = [1, 2, 3, 3, 2, 1], x=2
Output: 2
Explanation: Frequency of 2 is 2.
Try It Yourself
Table of Content
Using Linear Traversal - O(n) Time and O(1) Space
The idea is to traverse the array and count how many times the element x occurs.
Working of Approach:
- Initialize a variable cnt as 0.
- Traverse every element of the array.
- If the current element is equal to x, increment count.
- Return count after traversing the complete array.
Let us understand with an example:
Input: arr = [1, 2, 3, 3, 2, 1], x=2
- Traverse the array and compare each element with 2.
- When 2 is found at the second position, cnt becomes 1.
- When 2 is found again at the fifth position, cnt becomes 2.
- After traversing the array, return cnt = 2.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int findFrequency(vector<int> arr, int x)
{
int cnt = 0;
// Traverse the array and count occurrences of x
for (auto t : arr)
{
if (t == x)
cnt++;
}
return cnt;
}
int main()
{
vector<int> arr = {1, 2, 3, 3, 2, 1};
int x = 2;
cout << findFrequency(arr, x) << endl;
return 0;
}
import java.util.Arrays;
public class GFG {
public static int findFrequency(int[] arr, int x)
{
int cnt = 0;
// Traverse the array and count occurrences of x
for (int t : arr) {
if (t == x)
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 3, 2, 1 };
int x = 2;
System.out.println(findFrequency(arr, x));
}
}
def findFrequency(arr, x):
cnt = 0
# Traverse the array and count occurrences of x
for t in arr:
if t == x:
cnt += 1
return cnt
if __name__ == "__main__":
arr = [1, 2, 3, 3, 2, 1]
x = 2
print(findFrequency(arr, x))
using System;
using System.Linq;
public class GFG {
public static int findFrequency(int[] arr, int x)
{
int cnt = 0;
// Traverse the array and count occurrences of x
foreach(int t in arr)
{
if (t == x)
cnt++;
}
return cnt;
}
public static void Main()
{
int[] arr = { 1, 2, 3, 3, 2, 1 };
int x = 2;
Console.WriteLine(findFrequency(arr, x));
}
}
function findFrequency(arr, x)
{
let cnt = 0;
// Traverse the array and count occurrences of x
for (let t of arr) {
if (t === x)
cnt++;
}
return cnt;
}
// Driver Code
const arr = [ 1, 2, 3, 3, 2, 1 ];
const x = 2;
console.log(findFrequency(arr, x));
Output
2
Using Library Function
- C++: Use count(arr.begin(), arr.end(), x) to directly count occurrences of x.
- Java: Use Collections.frequency(list, x) to count occurrences of x.
- C#: Use arr.Count(t => t == x) with LINQ to count elements equal to x.
- Python: Use arr.count(x) to directly return the frequency of x.
- JavaScript: Use arr.filter(t => t === x).length to count elements equal to x.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int findFrequency(vector<int> arr, int x)
{
// Count occurrences of x using the STL function
return count(arr.begin(), arr.end(), x);
}
int main()
{
vector<int> arr = {1, 2, 3, 3, 2, 1};
int x = 2;
cout << findFrequency(arr, x) << endl;
return 0;
}
import java.util.Arrays;
public class GFG {
// Function to find frequency of x in the array
public static int findFrequency(int[] arr, int x)
{
// Count occurrences of x using the standard Java
// function
return (int)Arrays.stream(arr)
.filter(num -> num == x)
.count();
}
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 3, 2, 1 };
int x = 2;
System.out.println(findFrequency(arr, x));
}
}
def findFrequency(arr, x):
# Count occurrences of x using the built-in count method
return arr.count(x)
if __name__ == '__main__':
arr = [1, 2, 3, 3, 2, 1]
x = 2
print(findFrequency(arr, x))
using System;
using System.Linq;
public class GFG {
// Function to find frequency of x in the array
public static int findFrequency(int[] arr, int x)
{
// Count occurrences of x using the Linq function
return arr.Count(num = > num == x);
}
public static void Main()
{
int[] arr = { 1, 2, 3, 3, 2, 1 };
int x = 2;
Console.WriteLine(findFrequency(arr, x));
}
}
// Function to find frequency of x in the array
function findFrequency(arr, x)
{
// Count occurrences of x using the filter method
return arr.filter(num => num === x).length;
}
// Driver Code
const arr = [ 1, 2, 3, 3, 2, 1 ];
const x = 2;
console.log(findFrequency(arr, x));
Output
2
Frequency in a Sorted Array - O(log n) Time and O(1) Space
The idea is to use binary search to find the first and last occurrence of x in the sorted array, and use their positions to calculate its frequency.
Working of Approach:
- Use binary search to find the first occurrence of x.
- Use binary search again to find the last occurrence of x.
- If x is not present, return 0.
- Otherwise, the frequency is last - first + 1.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int findFrequency(vector<int> arr, int x)
{
int n = arr.size();
// Find the first occurrence of x
int low = 0, high = n - 1;
int first = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] == x)
{
first = mid;
high = mid - 1;
}
else if (arr[mid] < x)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
// If x is not present
if (first == -1)
return 0;
// Find the last occurrence of x
low = 0;
high = n - 1;
int last = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] == x)
{
last = mid;
low = mid + 1;
}
else if (arr[mid] < x)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
// Frequency = number of elements between first and last occurrence
return last - first + 1;
}
int main()
{
vector<int> arr = {1, 1, 2, 2, 2, 3, 3};
int x = 2;
cout << findFrequency(arr, x) << endl;
return 0;
}
import java.util.Arrays;
public class GFG {
public static int findFrequency(int[] arr, int x)
{
int n = arr.length;
// Find the first occurrence of x
int low = 0, high = n - 1;
int first = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
first = mid;
high = mid - 1;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
// If x is not present
if (first == -1)
return 0;
// Find the last occurrence of x
low = 0;
high = n - 1;
int last = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
last = mid;
low = mid + 1;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
// Frequency = number of elements between first and
// last occurrence
return last - first + 1;
}
public static void main(String[] args)
{
int[] arr = { 1, 1, 2, 2, 2, 3, 3 };
int x = 2;
System.out.println(findFrequency(arr, x));
}
}
def findFrequency(arr, x):
n = len(arr)
# Find the first occurrence of x
low = 0
high = n - 1
first = -1
while low <= high:
mid = low + (high - low) // 2
if arr[mid] == x:
first = mid
high = mid - 1
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
# If x is not present
if first == -1:
return 0
# Find the last occurrence of x
low = 0
high = n - 1
last = -1
while low <= high:
mid = low + (high - low) // 2
if arr[mid] == x:
last = mid
low = mid + 1
elif arr[mid] < x:
low = mid + 1
else:
high = mid - 1
# Frequency = number of elements between first and last occurrence
return last - first + 1
if __name__ == '__main__':
arr = [1, 1, 2, 2, 2, 3, 3]
x = 2
print(findFrequency(arr, x))
using System;
public class GFG {
public static int findFrequency(int[] arr, int x)
{
int n = arr.Length;
// Find the first occurrence of x
int low = 0, high = n - 1;
int first = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
first = mid;
high = mid - 1;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
// If x is not present
if (first == -1)
return 0;
// Find the last occurrence of x
low = 0;
high = n - 1;
int last = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
last = mid;
low = mid + 1;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
// Frequency = number of elements between first and
// last occurrence
return last - first + 1;
}
public static void Main()
{
int[] arr = { 1, 1, 2, 2, 2, 3, 3 };
int x = 2;
Console.WriteLine(findFrequency(arr, x));
}
}
function findFrequency(arr, x)
{
let n = arr.length;
// Find the first occurrence of x
let low = 0, high = n - 1;
let first = -1;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
if (arr[mid] === x) {
first = mid;
high = mid - 1;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
// If x is not present
if (first === -1)
return 0;
// Find the last occurrence of x
low = 0;
high = n - 1;
let last = -1;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
if (arr[mid] === x) {
last = mid;
low = mid + 1;
}
else if (arr[mid] < x) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
// Frequency = number of elements between first and last
// occurrence
return last - first + 1;
}
// Driver Code
let arr = [ 1, 1, 2, 2, 2, 3, 3 ];
let x = 2;
console.log(findFrequency(arr, x));
Output
3
Note: This approach is applicable only for a sorted array, as it uses binary search to find the first and last occurrence of x.