import java.util.ArrayList; import java.util.LinkedHashSet; /** * The Sequence class represents a collection of integers stored in an ArrayList. * It provides methods to add numbers, remove duplicate values, and display the sequence. */ private ArrayList<Integer> numbers; // List to store the sequence of numbers /** * Constructor to initialize an empty sequence. */ numbers = new ArrayList<>(); } /** * Adds a number to the sequence. * * @param num The integer to be added to the sequence. */ public void add(int num) { numbers.add(num); } /** * Removes duplicate values from the sequence while keeping only the first occurrence. * The original sequence is modified in place. * * This method does not return anything, but it updates the sequence. */ public void removeDuplicates() { // Using LinkedHashSet to remove duplicates while maintaining insertion order LinkedHashSet<Integer> uniqueNumbers = new LinkedHashSet<>(numbers); numbers.clear(); // Clear the original list numbers.addAll(uniqueNumbers); // Add back the unique numbers } /** * Prints the sequence to the console. * This method does not return anything. */ public void printSequence() { } } /** * The SequenceTester class tests the functionality of the Sequence class. * It creates multiple sequences, adds numbers, removes duplicates, and displays results. */ class SequenceTester { // Creating first sequence seq1.add(1); seq1.add(4); seq1.add(9); seq1.add(16); seq1.add(9); seq1.add(7); seq1.add(4); seq1.add(9); seq1.add(11); // Display original sequence seq1.printSequence(); // Remove duplicates and display updated sequence seq1.removeDuplicates(); seq1.printSequence(); // Creating second sequence seq2.add(3); seq2.add(3); seq2.add(3); seq2.add(5); seq2.add(7); seq2.add(7); seq2.printSequence(); seq2.removeDuplicates(); seq2.printSequence(); // Creating third sequence seq3.add(10); seq3.add(20); seq3.add(30); seq3.add(20); seq3.add(10); seq3.printSequence(); seq3.removeDuplicates(); seq3.printSequence(); } }
Standard input is empty
Original Sequence 1: [1, 4, 9, 16, 9, 7, 4, 9, 11] After removing duplicates: [1, 4, 9, 16, 7, 11] Original Sequence 2: [3, 3, 3, 5, 7, 7] After removing duplicates: [3, 5, 7] Original Sequence 3: [10, 20, 30, 20, 10] After removing duplicates: [10, 20, 30]