ISC Practicals and Theory - Array Deduplication
Updated 21 Oct 2026
Array deduplication means removing repeated values from an array so that every value appears only once. This program, written in the main method alone, reads an integer array of 1 to 20 elements, builds a new array that keeps the first occurrence of each value in the original order, and prints the new array and its new size. It uses only loops and arrays.
Definition
A value is a duplicate if it has already appeared earlier in the array. Removing duplicates keeps the first occurrence and drops every later one. The remaining elements stay in their original order, and the new size is the number of distinct values.
For example, for 4 7 4 2 7 9 2 4:
- The distinct values, in order of first appearance, are 4, 7, 2, 9.
- The new array is
4 7 2 9and the new size is 4.
Size constraint: the array size must be from 1 to 20.
Algorithm
- Read the number of elements
n. If it is not from 1 to 20, print an error message and stop. - Create the array
arrof sizenfor the input and the arrayresof sizenfor the result (it can never need more places thanarr). Setm = 0, the number of elements stored inres. - Read the
nelements intoarr. - For every element
arr[i], setfound = falseand search the filled part ofres(indices 0 tom - 1). Ifres[j] == arr[i], setfound = trueand stop searching. - If
foundis still false, the value is new: store it inres[m]and add 1 tom. - Print the first
melements ofresand the new sizem.
Program
The nested loops in main are the whole program: the outer loop takes each element of arr, and the inner loop searches res for it.
import java.util.Scanner;
public class ArrayDeduplication
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements (1 to 20): ");
int n = sc.nextInt(); // number of elements entered by the user (1 to 20)
if (n < 1 || n > 20) // size outside the allowed range
{
System.out.println("Invalid input. Size must be from 1 to 20.");
}
else
{
int[] arr = new int[n]; // original array as entered
int[] res = new int[n]; // new array holding each value once; it can never need more than n places
int m = 0; // number of elements stored in res (the new size)
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
for (int i = 0; i < n; i++)
{
boolean found = false; // becomes true if arr[i] is already in res
for (int j = 0; j < m; j++) // search only the part of res filled so far
{
if (res[j] == arr[i])
{
found = true;
break;
}
}
if (!found)
{
res[m] = arr[i]; // store the new value
m++; // one more element in res
}
}
System.out.println("Array after removing duplicates:");
for (int i = 0; i < m; i++)
{
System.out.print(res[i] + " ");
}
System.out.println();
System.out.println("New size = " + m);
}
}
}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 entered by the user (1 to 20) |
arr | int[] | The original array as entered |
res | int[] | The new array holding each value once, in order of first occurrence |
m | int | Number of elements stored in res, the new size |
i | int | Loop counter over the elements of arr (and over the filled part of res when printing) |
found | boolean | True if arr[i] is already in res |
j | int | Loop counter that searches the filled part of res |
Sample input and output
Sample 1
Enter the number of elements (1 to 20): 8
Enter the elements:
4 7 4 2 7 9 2 4
Array after removing duplicates:
4 7 2 9
New size = 4Sample 2
Enter the number of elements (1 to 20): 4
Enter the elements:
3 -1 0 9
Array after removing duplicates:
3 -1 0 9
New size = 4Sample 3
Enter the number of elements (1 to 20): 5
Enter the elements:
6 6 6 6 6
Array after removing duplicates:
6
New size = 1Sample 4
Enter the number of elements (1 to 20): 0
Invalid input. Size must be from 1 to 20.Dry run
Input: n = 8, arr = [4, 7, 4, 2, 7, 9, 2, 4]. At the start m = 0.
i | arr[i] | found | Action | res (first m places) | m |
|---|---|---|---|---|---|
| 0 | 4 | false | Store 4 | [4] | 1 |
| 1 | 7 | false | Store 7 | [4, 7] | 2 |
| 2 | 4 | true | Skip | [4, 7] | 2 |
| 3 | 2 | false | Store 2 | [4, 7, 2] | 3 |
| 4 | 7 | true | Skip | [4, 7, 2] | 3 |
| 5 | 9 | false | Store 9 | [4, 7, 2, 9] | 4 |
| 6 | 2 | true | Skip | [4, 7, 2, 9] | 4 |
| 7 | 4 | true | Skip | [4, 7, 2, 9] | 4 |
The new array is 4 7 2 9 and the new size is 4, which matches the first sample run.
Variations
- Sorted array "Remove the duplicate elements from a sorted array."
- Repeated elements "Display only the elements that occur more than once in an array."
- Frequency count "Display each distinct element of an array with the number of times it occurs."
- Keep last occurrence "Remove duplicates from an array keeping the last occurrence of each value."