Frequency of an Element in an Array: flowchart, algorithm and program

This program reads n numbers into an array, then reads a key and prints how many times that key appears. It walks through the array once, and every time a[i] equals key it adds 1 to count.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set count = 0.
  3. Step 3: Read n.
  4. Step 4: Set i = 0.
  5. Step 5: If i < n, go to step 6; otherwise go to step 8.
  6. Step 6: Read a[i].
  7. Step 7: Increase i by 1. Go to step 5.
  8. Step 8: Read key.
  9. Step 9: Set i = 0.
  10. Step 10: If i < n, go to step 11; otherwise go to step 14.
  11. Step 11: If a[i] == key, go to step 12; otherwise go to step 13.
  12. Step 12: Increase count by 1.
  13. Step 13: Increase i by 1. Go to step 10.
  14. Step 14: Print key, count.
  15. Step 15: Stop.

Pseudocode

BEGIN
    count ← 0
    READ n
    i ← 0
    WHILE i < n DO
        READ a[i]
        i++
    END WHILE
    READ key
    i ← 0
    WHILE i < n DO
        IF a[i] == key THEN
            count++
        END IF
        i++
    END WHILE
    PRINT key, count
END

Program in C, Python and Java

Frequency of an Element in an Array program in C

#include <stdio.h>

int main() {
    int n, i, key, count = 0;
    int a[100];

    printf("Enter n: ");
    scanf("%d", &n);

    printf("Enter %d numbers: ", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }

    printf("Enter key: ");
    scanf("%d", &key);

    for (i = 0; i < n; i++) {
        if (a[i] == key) {
            count++;
        }
    }

    printf("Frequency of %d = %d\n", key, count);
    return 0;
}

Frequency of an Element in an Array program in Python

n = int(input("Enter n: "))
a = list(map(int, input("Enter " + str(n) + " numbers: ").split()))
key = int(input("Enter key: "))

count = 0
for i in range(n):
    if a[i] == key:
        count += 1

print("Frequency of", key, "=", count)

Frequency of an Element in an Array program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n, i, key, count = 0;

        System.out.print("Enter n: ");
        n = sc.nextInt();
        int[] a = new int[n];

        System.out.print("Enter " + n + " numbers: ");
        for (i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }

        System.out.print("Enter key: ");
        key = sc.nextInt();

        for (i = 0; i < n; i++) {
            if (a[i] == key) {
                count++;
            }
        }

        System.out.println("Frequency of " + key + " = " + count);
    }
}

Sample input: n = 7, numbers = 2 5 3 5 8 5 1, key = 5
Output: Frequency of 5 = 3

How it works

  • count starts at 0 because before looking at any element, the key has been seen zero times.
  • The first for loop reads n numbers into a[0] to a[n-1]. After that, the key is read with scanf.
  • The second for loop visits every index i from 0 to n-1 and checks if (a[i] == key).
  • Each time the check is true, count++ adds one. Nothing happens when it is false, so the loop simply moves on.
  • After the loop ends, count holds the total number of matches, and it is printed together with the key.
  • For 2 5 3 5 8 5 1 and key 5, the matches are at index 1, 3 and 5, so count ends at 3.

Dry run (trace table)

StepLineBoxcountnia[0]a[1]a[2]a[3]a[4]a[5]a[6]keyOutput
13Start
24count = 00
38Read n07
411i = 0070
511i < n?070
612Read a[i]0702
711i++0712
811i < n?071
912Read a[i]0715
1011i++072
1111i < n?072
1212Read a[i]0723
1311i++073
1411i < n?073
1512Read a[i]0735
1611i++074
1711i < n?074
1812Read a[i]0748
1911i++075
2011i < n?075

Time and space complexity

Time: O(n) · Space: O(n). Each loop runs n times with constant work per iteration, and the array stores n elements.

Common mistakes

  • Forgetting to set count = 0 at the start, so it begins with a garbage value and the answer is wrong.
  • Using = instead of == in the if condition. That assigns key to a[i] instead of comparing, and it changes the array.
  • Stopping the loop at the first match (with break) or printing inside the loop. That gives 1 or repeated output instead of the total count.
  • Entering n larger than 100, which writes outside the array a[100].

Viva questions

Q1. Why is count initialised to 0 on line 4?

count is a running tally of matches, so it must start at 0 before any element is checked. Without it, count would hold a garbage value and the final frequency would be wrong.

Q2. What does the condition a[i] == key on line 19 check, and what is the difference between == and =?

It checks whether the current array element equals the key, and if so count is increased. == compares two values, while = assigns; writing a[i] = key would overwrite the array and always be true unless key is 0.

Q3. What will the program print if the key is not present in the array?

It prints 'Frequency of key = 0', because the if condition is never true and count stays at its initial value 0.

Q4. What happens if the user enters n = 0, or n larger than 100?

With n = 0 both loops are skipped and the frequency printed is 0, which is correct. With n above 100 the loop writes past the end of a[100], causing undefined behaviour, so the program should validate n first.

Q5. Why do we use a for loop here, and why do we need two separate loops?

We know the number of iterations (n), so a for loop fits. The first loop reads the input into the array and the second scans it; the key is only read after the array is filled, so the counting could not be merged into the first loop as written.

Q6. Why is there an & before a[i] and key in scanf but not before n in printf?

scanf needs the address of the variable so it can store the value there, hence &a[i] and &key. printf only needs the value to display, so no & is used.

Q7. What is the time and space complexity of this program?

Time is O(n) because the array is read once and scanned once, each a single pass over n elements. Space is O(1) extra beyond the array, since only a few integer variables are used.

Q8. What would change if you wanted the frequency of every distinct element, not just one key?

A single count variable is not enough. You could use nested loops (O(n^2)) that count each element and skip already-counted ones, or use a frequency array or hash/sorting to get it in O(n) or O(n log n).

Frequently asked questions

What is the time complexity of finding the frequency of an element in an array?

It is O(n), because the loop checks each of the n elements exactly once. It uses O(1) extra space apart from the array itself.

What does the program print if the key is not in the array?

count never increases, so it stays 0 and the program prints Frequency of key = 0.

How is this different from linear search?

Linear search can stop at the first match. Frequency counting must check every element to the end, since more matches may appear later.

More programs

← All programs

Try AI Flowchart free →