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

ISC Practicals and Theory - Frequency Table (OOP)

Updated 21 Oct 2026

A frequency table lists each distinct value of an array once, with the number of times it occurs. This version is written as a class: the parameterised constructor receives the array, and separate methods search the table, count and display. It reads 1 to 10 integers and prints the values in the order in which they first appear.

Definition

The frequency of a value is the number of array positions holding it. For $\{3, 1, 3, 3, 1\}$ the frequency of 3 is 3 and of 1 is 2.

  • 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.
  • The table is in first-appearance order, not sorted.

Algorithm

  1. In main, read size. If it is outside 1 to 10, print an error message and stop.
  2. Read the elements into x and create the object with new Frequency(x, size). The constructor stores the array in arr, the size in n, creates val and freq of size n and sets d = 0.
  3. Call count(): for each arr[i], find() returns its position in the first d entries of val, or -1.
    • If -1, store the value in val[d], set freq[d] = 1 and increase d.
    • Otherwise increase freq[pos] by 1.
  4. Call display() to print each value with its frequency.

Program

The constructor sets up the table arrays, find() searches the table, count() builds it and display() prints it. Reading and the size check are done in main.

import java.util.Scanner;

/* class Frequency: holds an integer array and prints how many times each distinct value
   occurs, in the order in which the values first appear (no collections or Arrays class) */
public class Frequency
{
    int arr[];    // the numbers entered
    int n;        // number of elements in arr
    int val[];    // each distinct value, in first-appearance order
    int freq[];   // freq[k] = how many times val[k] occurs in arr
    int d;        // number of distinct values found

    /* Parameterised constructor.
       Parameters: x - the array of numbers, size - number of elements in x */
    Frequency(int x[], int size)
    {
        arr = x;
        n = size;
        val = new int[n];      // there can be at most n distinct values
        freq = new int[n];
        d = 0;
    }

    /* Parameter: x - the value to look for.
       Returns the position of x in val (among the first d entries), or -1 if absent */
    int find(int x)
    {
        for (int k = 0; k < d; k++)
        {
            if (val[k] == x)
                return k;
        }
        return -1;
    }

    /* Fills val and freq: for each element of arr, add 1 to the count of its
       value if it is already listed, otherwise list it with count 1 */
    void count()
    {
        for (int i = 0; i < n; i++)
        {
            int pos = find(arr[i]);
            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]++;
        }
    }

    /* Displays the frequency table */
    void display()
    {
        System.out.println("Value\tFrequency");
        for (int k = 0; k < d; k++)
        {
            System.out.println(val[k] + "\t" + freq[k]);
        }
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of elements (1 to 10): ");
        int size = sc.nextInt();
        if (size < 1 || size > 10)
        {
            System.out.println("Invalid input. Size must be from 1 to 10.");
            return;
        }
        int x[] = new int[size];
        System.out.println("Enter the elements:");
        for (int i = 0; i < size; i++)
            x[i] = sc.nextInt();

        Frequency ob = new Frequency(x, size);
        ob.count();
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
arrint[]Data member: the numbers entered
nintData member: number of elements in arr
valint[]Data member: each distinct value, in first-appearance order
freqint[]Data member: freq[k] is the number of times val[k] occurs
dintData member: number of distinct values found so far
xint[]Constructor parameter and local in main: the array of numbers; in find() it is an int, the value to look for
sizeintConstructor parameter and local in main: number of elements
iintLoop counter over arr in count() and over the input in main
kintLoop counter over the table in find() and display()
posintIn count(): position of the value in val, or -1 if it is new
scScannerScanner object to read input in main
argsString[]Parameter: command line arguments of main
obFrequencyObject of the class created in main

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: size = 6, x = {-1, 5, -1, 0, 5, 0}.

iarr[i]posActionvalfreqd
0-1-1new value-111
15-1new value-1 51 12
2-10freq[0]++-1 52 12
30-1new value-1 5 02 1 13
451freq[1]++-1 5 02 2 13
502freq[2]++-1 5 02 2 23

The table printed is -1 with 2, 5 with 2 and 0 with 2, which matches the fourth 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."