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
- In
main, readsize. If it is outside 1 to 10, print an error message and stop. - Read the elements into
xand create the object withnew Frequency(x, size). The constructor stores the array inarr, the size inn, createsvalandfreqof sizenand setsd = 0. - Call
count(): for eacharr[i],find()returns its position in the firstdentries ofval, or -1.- If -1, store the value in
val[d], setfreq[d] = 1and increased. - Otherwise increase
freq[pos]by 1.
- If -1, store the value in
- 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
| Variable | Data Type | Purpose |
|---|---|---|
arr | int[] | Data member: the numbers entered |
n | int | Data member: number of elements in arr |
val | int[] | Data member: each distinct value, in first-appearance order |
freq | int[] | Data member: freq[k] is the number of times val[k] occurs |
d | int | Data member: number of distinct values found so far |
x | int[] | Constructor parameter and local in main: the array of numbers; in find() it is an int, the value to look for |
size | int | Constructor parameter and local in main: number of elements |
i | int | Loop counter over arr in count() and over the input in main |
k | int | Loop counter over the table in find() and display() |
pos | int | In count(): position of the value in val, or -1 if it is new |
sc | Scanner | Scanner object to read input in main |
args | String[] | Parameter: command line arguments of main |
ob | Frequency | Object 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 1Sample 2
Enter the number of elements (1 to 10): 1
Enter the elements:
6
Value Frequency
6 1Sample 3
Enter the number of elements (1 to 10): 5
Enter the elements:
3 3 3 3 3
Value Frequency
3 5Sample 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 2Sample 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.Dry run
Input: size = 6, x = {-1, 5, -1, 0, 5, 0}.
i | arr[i] | pos | Action | val | freq | d |
|---|---|---|---|---|---|---|
| 0 | -1 | -1 | new value | -1 | 1 | 1 |
| 1 | 5 | -1 | new value | -1 5 | 1 1 | 2 |
| 2 | -1 | 0 | freq[0]++ | -1 5 | 2 1 | 2 |
| 3 | 0 | -1 | new value | -1 5 0 | 2 1 1 | 3 |
| 4 | 5 | 1 | freq[1]++ | -1 5 0 | 2 2 1 | 3 |
| 5 | 0 | 2 | freq[2]++ | -1 5 0 | 2 2 2 | 3 |
The table printed is -1 with 2, 5 with 2 and 0 with 2, which matches the fourth sample run.
Variations
- Digit frequency "Accept a number and display the frequency of each digit 0 to 9 in it."
- Sorted table "Display the frequency table with the values in ascending order."
- Mode "Display the element that occurs the most number of times in an array."