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
- Read the number of elements
n. If it is not from 1 to 10, print an error message and stop. - Read the
nelements into the arrayarr. - Create two parallel arrays of size
n:valholds each distinct value in the order it first appears, andfreq[k]holds how many timesval[k]occurs. Setd = 0, the number of distinct values found so far. - For every element
arr[i], search the firstdplaces ofvalfor it and note its position inpos(-1 if it is not there).- If
pos == -1, this is the first time the value is seen: store it inval[d], setfreq[d] = 1and add 1 tod. - Otherwise add 1 to
freq[pos].
- If
- Print the heading and then, for
kfrom 0 tod - 1, the valueval[k]and its frequencyfreq[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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
n | int | Number of elements in arr (1 to 10) |
arr | int[] | The numbers entered |
val | int[] | Each distinct value, in first-appearance order |
freq | int[] | freq[k] is how many times val[k] occurs in arr |
d | int | Number of distinct values found |
i | int | Loop counter over the elements of arr |
pos | int | Position of arr[i] in val, -1 if it is not there yet |
k | int | Loop 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 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: n = 8, arr = {4, 2, 4, 7, 2, 4, 9, 7}.
i | arr[i] | pos | Action | val | freq | d |
|---|---|---|---|---|---|---|
| 0 | 4 | -1 | new value | 4 | 1 | 1 |
| 1 | 2 | -1 | new value | 4 2 | 1 1 | 2 |
| 2 | 4 | 0 | freq[0]++ | 4 2 | 2 1 | 2 |
| 3 | 7 | -1 | new value | 4 2 7 | 2 1 1 | 3 |
| 4 | 2 | 1 | freq[1]++ | 4 2 7 | 2 2 1 | 3 |
| 5 | 4 | 0 | freq[0]++ | 4 2 7 | 3 2 1 | 3 |
| 6 | 9 | -1 | new value | 4 2 7 9 | 3 2 1 1 | 4 |
| 7 | 7 | 2 | freq[2]++ | 4 2 7 9 | 3 2 2 1 | 4 |
The table printed is 4 with 3, 2 with 2, 7 with 2 and 9 with 1, which matches the first 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."
- Repeated elements "Display only the elements that occur more than once in an array."