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

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

  1. In main, read the size into size. If size < 1 or size > 20, print an error message and stop.
  2. Create the object obj with new ArrayDeduplicationOOP(size). The constructor sets n = size, creates arr and res of size n and sets m = 0.
  3. readElements(sc) reads n integers into arr.
  4. removeDuplicates() takes each arr[i] from left to right:
    • It calls isPresent(arr[i]), which searches res[0] to res[m - 1] and returns true if the value is found, otherwise false.
    • If the value is not present, it stores res[m] = arr[i] and increases m by 1.
  5. display() prints res[0] to res[m - 1] and then the new size m.

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

VariableData TypePurpose
nintData member: number of elements in the array
arrint[]Data member: original array as entered
resint[]Data member: new array holding each value once, in order of first occurrence
mintData member: number of elements stored in res, which is the new size
sizeintSize read in main; also the parameter of the constructor
scScannerScanner object in main to read input; also the parameter of readElements()
iintLoop counter for reading arr, for scanning arr in removeDuplicates() and for printing res
jintLoop counter in isPresent() that searches the filled part of res
vintParameter of isPresent(): the value to search for
objArrayDeduplicationOOPObject created in main to call the methods
argsString[]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 = 4

Sample 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 = 4

Sample 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 = 1

Sample 4

Enter the number of elements (1 to 20): 21
Invalid input. Size must be from 1 to 20.

One value per line

Dry run

Input: size = 8, arr = [4, 7, 4, 2, 7, 9, 2, 4]. The constructor sets m = 0.

iarr[i]isPresent(arr[i])Actionres (first m places)m
04falseStore 4[4]1
17falseStore 7[4, 7]2
24trueSkip[4, 7]2
32falseStore 2[4, 7, 2]3
47trueSkip[4, 7, 2]3
59falseStore 9[4, 7, 2, 9]4
62trueSkip[4, 7, 2, 9]4
74trueSkip[4, 7, 2, 9]4

display() prints 4 7 2 9 and New size = 4, which matches the first sample run.

Variations

  1. Sorted array "Remove the duplicate elements from a sorted array."
  2. Repeated elements "Display only the elements that occur more than once in an array."
  3. Frequency count "Display each distinct element of an array with the number of times it occurs."
  4. Keep last occurrence "Remove duplicates from an array keeping the last occurrence of each value."