ISC Practicals and Theory - Array Deduplication (OOP)
Updated 21 Oct 2026
Array deduplication removes repeated values from an array so that each value appears once. This version is written as a class: the size goes to a parameterised constructor, and separate methods read the array, test whether a value is already present, build the new array and display it. It reads 1 to 20 integers and prints the new array and its new size.
Definition
A value is a duplicate if it has already appeared earlier in the array. The program keeps the first occurrence of each value and preserves the original order. For 4 7 4 2 7 9 2 4 the new array is 4 7 2 9 and the new size is 4.
Algorithm
- In
main, read the size intosize. Ifsize < 1orsize > 20, print an error message and stop. - Create the object
objwithnew ArrayDeduplicationOOP(size). The constructor setsn = size, createsarrandresof sizenand setsm = 0. readElements(sc)readsnintegers intoarr.removeDuplicates()takes eacharr[i]from left to right:- It calls
isPresent(arr[i]), which searchesres[0]tores[m - 1]and returnstrueif the value is found, otherwisefalse. - If the value is not present, it stores
res[m] = arr[i]and increasesmby 1.
- It calls
display()printsres[0]tores[m - 1]and then the new sizem.
Program
The class ArrayDeduplicationOOP has the data members n, arr, res and m. The constructor prepares the arrays, readElements() reads, isPresent() searches, removeDuplicates() builds the new array and display() prints it.
import java.util.Scanner;
/**
* Class ArrayDeduplicationOOP removes duplicate values from an integer array.
* The first occurrence of every value is kept and the original order is preserved.
* The size is passed to the parameterised constructor.
*/
public class ArrayDeduplicationOOP
{
int n; // number of elements in the array (1 to 20)
int[] arr; // original array as entered
int[] res; // new array holding each value once, in order of first occurrence
int m; // number of elements stored in res (the new size)
/**
* Parameterised constructor: sets the size and creates both arrays.
* @param size the number of elements, already checked to be from 1 to 20
*/
ArrayDeduplicationOOP(int size)
{
n = size;
arr = new int[n];
res = new int[n]; // res can never need more than n places
m = 0; // nothing stored in res yet
}
/**
* Reads the n elements into arr.
* @param sc the Scanner used to read input
*/
void readElements(Scanner sc)
{
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
}
/**
* Checks whether a value is already stored in the filled part of res.
* @param v the value to search for
* @return true if v is present in res[0] to res[m - 1], false otherwise
*/
boolean isPresent(int v)
{
for (int j = 0; j < m; j++)
{
if (res[j] == v)
{
return true;
}
}
return false;
}
/**
* Copies each value of arr into res the first time it is seen.
*/
void removeDuplicates()
{
for (int i = 0; i < n; i++)
{
if (!isPresent(arr[i])) // keep only the first occurrence
{
res[m] = arr[i];
m++;
}
}
}
/**
* Displays the new array and its new size.
*/
void display()
{
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);
}
/**
* Reads and checks the size, creates the object and calls the methods.
* @param args command line arguments (not used)
*/
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements (1 to 20): ");
int size = sc.nextInt();
if (size < 1 || size > 20)
{
System.out.println("Invalid input. Size must be from 1 to 20.");
return;
}
ArrayDeduplicationOOP obj = new ArrayDeduplicationOOP(size);
obj.readElements(sc);
obj.removeDuplicates();
obj.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member: number of elements in the array |
arr | int[] | Data member: original array as entered |
res | int[] | Data member: new array holding each value once, in order of first occurrence |
m | int | Data member: number of elements stored in res, which is the new size |
size | int | Size read in main; also the parameter of the constructor |
sc | Scanner | Scanner object in main to read input; also the parameter of readElements() |
i | int | Loop counter for reading arr, for scanning arr in removeDuplicates() and for printing res |
j | int | Loop counter in isPresent() that searches the filled part of res |
v | int | Parameter of isPresent(): the value to search for |
obj | ArrayDeduplicationOOP | Object created in main to call the methods |
args | String[] | Parameter: command line arguments of main |
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): 21
Invalid input. Size must be from 1 to 20.Dry run
Input: size = 8, arr = [4, 7, 4, 2, 7, 9, 2, 4]. The constructor sets m = 0.
i | arr[i] | isPresent(arr[i]) | 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 |
display() prints 4 7 2 9 and New size = 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."