Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Frequency Table

Updated 21 Oct 2026

A frequency table lists each distinct value of a data set once, together with the number of times it occurs. This program, written in the main method alone, reads an integer array of 1 to 10 elements, counts the occurrences of every distinct value using two parallel arrays, and prints the table with the values in the order in which they first appear in the input. No collection classes and no Arrays methods are used.

Definition

The frequency of a value is the number of positions of the array that hold that value. For the array $\{4, 2, 4, 7, 2, 4, 9, 7\}$ the frequency of 4 is 3, of 2 is 2, of 7 is 2 and of 9 is 1, and the frequencies add up to the array size, $3 + 2 + 2 + 1 = 8$.

  • The program works on an integer array (not on the digits of a number).
  • The size must be from 1 to 10, otherwise an error message is printed.
  • Each distinct value is printed once, in first-appearance order (not sorted). Negative values and zero are allowed.

Algorithm

  1. Read the number of elements n. If it is not from 1 to 10, print an error message and stop.
  2. Read the n elements into the array arr.
  3. Create two parallel arrays of size n: val holds each distinct value in the order it first appears, and freq[k] holds how many times val[k] occurs. Set d = 0, the number of distinct values found so far.
  4. For every element arr[i], search the first d places of val for it and note its position in pos (-1 if it is not there).
    • If pos == -1, this is the first time the value is seen: store it in val[d], set freq[d] = 1 and add 1 to d.
    • Otherwise add 1 to freq[pos].
  5. Print the heading and then, for k from 0 to d - 1, the value val[k] and its frequency freq[k].

Program

In main, the outer loop takes each element of arr and the inner loop searches the distinct values found so far; the last loop prints the table.

import java.util.Scanner;

/* Program Frequency displays each distinct value of an array with the number of times it occurs,
   in the order in which the values first appear (no collections or Arrays class) */
public class Frequency
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of elements (1 to 10): ");
        int n = sc.nextInt();                  // number of elements in arr
        if (n < 1 || n > 10)                   // the size must be from 1 to 10
        {
            System.out.println("Invalid input. Size must be from 1 to 10.");
            return;
        }
        int arr[] = new int[n];                // the numbers entered
        System.out.println("Enter the elements:");
        for (int i = 0; i < n; i++)
        {
            arr[i] = sc.nextInt();
        }
        int val[] = new int[n];                // each distinct value, in first-appearance order (at most n)
        int freq[] = new int[n];               // freq[k] = how many times val[k] occurs in arr
        int d = 0;                             // number of distinct values found
        for (int i = 0; i < n; i++)
        {
            int pos = -1;                      // position of arr[i] in val, -1 if absent
            for (int k = 0; k < d; k++)
            {
                if (val[k] == arr[i])
                {
                    pos = k;
                    break;
                }
            }
            if (pos == -1)                     // first time this value is seen
            {
                val[d] = arr[i];
                freq[d] = 1;
                d++;
            }
            else                               // seen before: one more occurrence
                freq[pos]++;
        }
        System.out.println("Value\tFrequency");
        for (int k = 0; k < d; k++)
        {
            System.out.println(val[k] + "\t" + freq[k]);
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
nintNumber of elements in arr (1 to 10)
arrint[]The numbers entered
valint[]Each distinct value, in first-appearance order
freqint[]freq[k] is how many times val[k] occurs in arr
dintNumber of distinct values found
iintLoop counter over the elements of arr
posintPosition of arr[i] in val, -1 if it is not there yet
kintLoop counter over the distinct values

Sample input and output

Sample 1

Enter the number of elements (1 to 10): 8
Enter the elements:
4 2 4 7 2 4 9 7
Value	Frequency
4	3
2	2
7	2
9	1

Sample 2

Enter the number of elements (1 to 10): 1
Enter the elements:
6
Value	Frequency
6	1

Sample 3

Enter the number of elements (1 to 10): 5
Enter the elements:
3 3 3 3 3
Value	Frequency
3	5

Sample 4

Enter the number of elements (1 to 10): 6
Enter the elements:
-1 5 -1 0 5 0
Value	Frequency
-1	2
5	2
0	2

Sample 5

Enter the number of elements (1 to 10): 11
Invalid input. Size must be from 1 to 10.

Sample 6

Enter the number of elements (1 to 10): 0
Invalid input. Size must be from 1 to 10.

One value per line

Dry run

Input: n = 8, arr = {4, 2, 4, 7, 2, 4, 9, 7}.

iarr[i]posActionvalfreqd
04-1new value411
12-1new value4 21 12
240freq[0]++4 22 12
37-1new value4 2 72 1 13
421freq[1]++4 2 72 2 13
540freq[0]++4 2 73 2 13
69-1new value4 2 7 93 2 1 14
772freq[2]++4 2 7 93 2 2 14

The table printed is 4 with 3, 2 with 2, 7 with 2 and 9 with 1, which matches the first sample run.

Variations

  1. Digit frequency "Accept a number and display the frequency of each digit 0 to 9 in it."
  2. Sorted table "Display the frequency table with the values in ascending order."
  3. Mode "Display the element that occurs the most number of times in an array."
  4. Repeated elements "Display only the elements that occur more than once in an array."