Find frequency of each element in a limited range array in less than O(n) time (original) (raw)

Last Updated : 23 Jul, 2025

Given a **sorted array arr[] of positive integers, the task is to find the frequency for each element in the array. Assume all elements in the array are less than some constant **M

**Note: Do this without traversing the complete array. i.e. expected time complexity is less than O(n)

**Examples:

**Input: arr[] = [1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10]
**Output:
Element 1 occurs 3 times
Element 2 occurs 1 times
Element 3 occurs 2 times
Element 5 occurs 2 times
Element 8 occurs 3 times
Element 9 occurs 2 times
Element 10 occurs 1 times

**Input: arr[] = [2, 2, 6, 6, 7, 7, 7, 11]
**Output:
Element 2 occurs 2 times
Element 6 occurs 2 times
Element 7 occurs 3 times
Element 11 occurs 1 times

**[Naive Approach - 1] Using Linear Search - O(n) time and O(1) space

To solve the problem follow the below idea:

Traverse the input array and increment the frequency of the element if the current element and the previous element are the same, otherwise reset the frequency and print the element and its frequency

Follow the given steps to solve the problem:

// C++ program to count number of occurrences of // each value in the array in O(n) time and O(1) space #include <bits/stdc++.h> using namespace std;

void findFrequencies(vector &arr) { int n = arr.size(); int freq = 1; int idx = 1; int value = arr[0]; while (idx < n) {

    // check if the current value is equal to
    // previous value.
    if (arr[idx - 1] == arr[idx]) {
        freq++;
        idx++;
    }
    else {
        cout << value << " " << freq << endl;
        value = arr[idx];
        idx++;

        // reset the frequency
        freq = 1;
    }
}

// print the last value and its frequency
cout << value << " " << freq;

}

int main() { vector arr = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10};

findFrequencies(arr);
return 0;

}

Java

// Java program to count number of occurrences of // each value in the array in O(n) time and O(1) space

class GfG {

static void findFrequencies(int[] arr) {
    int n = arr.length;
    int freq = 1;
    int idx = 1;
    int value = arr[0];
    while (idx < n) {

        // check if the current value is equal to
        // previous value.
        if (arr[idx - 1] == arr[idx]) {
            freq++;
            idx++;
        }
        else {
            System.out.println(value + " " + freq);
            value = arr[idx];
            idx++;

            // reset the frequency
            freq = 1;
        }
    }

    // print the last value and its frequency
    System.out.println(value + " " + freq);
}

public static void main(String[] args) {
    int[] arr = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10};

    findFrequencies(arr);
}

}

Python

Python program to count number of occurrences of

each value in the array in O(n) time and O(1) space

def findFrequencies(arr): n = len(arr) freq = 1 idx = 1 value = arr[0] while idx < n:

    # check if the current value is equal to
    # previous value.
    if arr[idx - 1] == arr[idx]:
        freq += 1
        idx += 1
    else:
        print(value, freq)
        value = arr[idx]
        idx += 1

        # reset the frequency
        freq = 1

# print the last value and its frequency
print(value, freq)

if name == "main": arr = [1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10]

findFrequencies(arr)

C#

// C# program to count number of occurrences of // each value in the array in O(n) time and O(1) space

using System;

class GfG {

static void findFrequencies(int[] arr) {
    int n = arr.Length;
    int freq = 1;
    int idx = 1;
    int value = arr[0];
    while (idx < n) {

        // check if the current value is equal to
        // previous value.
        if (arr[idx - 1] == arr[idx]) {
            freq++;
            idx++;
        }
        else {
            Console.WriteLine(value + " " + freq);
            value = arr[idx];
            idx++;

            // reset the frequency
            freq = 1;
        }
    }

    // print the last value and its frequency
    Console.WriteLine(value + " " + freq);
}

static void Main() {
    int[] arr = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10};

    findFrequencies(arr);
}

}

JavaScript

// JavaScript program to count number of occurrences of // each value in the array in O(n) time and O(1) space

function findFrequencies(arr) { let n = arr.length; let freq = 1; let idx = 1; let value = arr[0]; while (idx < n) {

    // check if the current value is equal to
    // previous value.
    if (arr[idx - 1] === arr[idx]) {
        freq++;
        idx++;
    }
    else {
        console.log(value + " " + freq);
        value = arr[idx];
        idx++;

        // reset the frequency
        freq = 1;
    }
}

// print the last value and its frequency
console.log(value + " " + freq);

}

let arr = [1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10];

findFrequencies(arr);

`

Output

1 3 2 1 3 2 5 2 8 3 9 2 10 1

**[Expected Approach] Using Binary Search - O(m log(n)) time and O(1) space

The idea is to leverage the sorted nature of the array by using binary search to find the last occurrence of each unique element.

**Step by step approach:

// C++ program to count number of occurrences of // each value in the array in O(n) time and O(1) space #include <bits/stdc++.h> using namespace std;

// It print number of // occurrences of each element in the array. void findFrequencies(vector &arr) { int n = arr.size();

int i = 0;

while (i<n) {
    int val = arr[i];
    int s = i, e = n-1;
    int endIndex = i;
    
    while (s<=e) {
        int mid = s + (e-s)/2;
        if (arr[mid] == val) {
            endIndex = mid;
            s = mid + 1;
        }
        else {
            e = mid - 1;
        }
    }
    
    int cnt = endIndex - i + 1;
    
    cout << val << " " << cnt << endl;
    
    // Set i to next value index
    i = endIndex + 1;
}

}

int main() { vector arr = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10};

findFrequencies(arr);
return 0;

}

Java

// Java program to count number of occurrences of // each value in the array in O(n) time and O(1) space

class GfG {

// It prints number of
// occurrences of each element in the array.
static void findFrequencies(int[] arr) {
    int n = arr.length;
    
    int i = 0;
    
    while (i < n) {
        int val = arr[i];
        int s = i, e = n - 1;
        int endIndex = i;
        
        while (s <= e) {
            int mid = s + (e - s) / 2;
            if (arr[mid] == val) {
                endIndex = mid;
                s = mid + 1;
            }
            else {
                e = mid - 1;
            }
        }
        
        int cnt = endIndex - i + 1;
        
        System.out.println(val + " " + cnt);
        
        // Set i to next value index
        i = endIndex + 1;
    }
}

public static void main(String[] args) {
    int[] arr = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10};

    findFrequencies(arr);
}

}

Python

Python program to count number of occurrences of

each value in the array in O(n) time and O(1) space

It prints number of

occurrences of each element in the array.

def findFrequencies(arr): n = len(arr)

i = 0

while i < n:
    val = arr[i]
    s, e = i, n - 1
    endIndex = i
    
    while s <= e:
        mid = s + (e - s) // 2
        if arr[mid] == val:
            endIndex = mid
            s = mid + 1
        else:
            e = mid - 1
    
    cnt = endIndex - i + 1
    
    print(val, cnt)
    
    # Set i to next value index
    i = endIndex + 1

if name == "main": arr = [1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10]

findFrequencies(arr)

C#

// C# program to count number of occurrences of // each value in the array in O(n) time and O(1) space

using System;

class GfG {

// It prints number of
// occurrences of each element in the array.
static void findFrequencies(int[] arr) {
    int n = arr.Length;
    
    int i = 0;
    
    while (i < n) {
        int val = arr[i];
        int s = i, e = n - 1;
        int endIndex = i;
        
        while (s <= e) {
            int mid = s + (e - s) / 2;
            if (arr[mid] == val) {
                endIndex = mid;
                s = mid + 1;
            }
            else {
                e = mid - 1;
            }
        }
        
        int cnt = endIndex - i + 1;
        
        Console.WriteLine(val + " " + cnt);
        
        // Set i to next value index
        i = endIndex + 1;
    }
}

static void Main() {
    int[] arr = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10};

    findFrequencies(arr);
}

}

JavaScript

// JavaScript program to count number of occurrences of // each value in the array in O(n) time and O(1) space

// It prints number of // occurrences of each element in the array. function findFrequencies(arr) { let n = arr.length;

let i = 0;

while (i < n) {
    let val = arr[i];
    let s = i, e = n - 1;
    let endIndex = i;
    
    while (s <= e) {
        let mid = Math.floor(s + (e - s) / 2);
        if (arr[mid] === val) {
            endIndex = mid;
            s = mid + 1;
        }
        else {
            e = mid - 1;
        }
    }
    
    let cnt = endIndex - i + 1;
    
    console.log(val + " " + cnt);
    
    // Set i to next value index
    i = endIndex + 1;
}

}

let arr = [1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10];

findFrequencies(arr);

`

Output

1 3 2 1 3 2 5 2 8 3 9 2 10 1

**Time Complexity: O(m log n). Where m is the number of distinct elements in the array of size n. Since m <= M (a constant) (elements are in a limited range), the time complexity of this solution is O(log N)
**Auxiliary Space: O(1)