VOOZH about

URL: https://www.geeksforgeeks.org/java/design-a-ds-for-remove-and-print/

⇱ Design a DS for Remove and Print - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Design a DS for Remove and Print

Last Updated : 31 Jan, 2026

Design a data structure that supports adding elements and removing all occurrences of a given element, while printing the remaining elements in their original insertion order.

  • add(int x): Inserts the element x into the data structure.
  • removeAndPrint(int x): Removes all occurrences of x and prints the remaining elements in insertion order.

Example:

Input:
add(10), add(20), add(10), add(30), removeAndPrint(10)
add(30), add(40), add(60), removeAndPrint(30)
Output:
20 30
20 40 60

Using ArrayList vs LinkedList

We can use ArrayList or LinkedList to implement this data structure. Below is a comparison:

ArrayList Approach:

  • Add Operation: O(1) (Amortized Time Complexity)
  • RemoveAndPrint Operation: O(n) (Since shifting is required after removal)

LinkedList Approach (Preferred Solution):

  • Add Operation: O(1) (Worst case as well)
  • RemoveAndPrint Operation: O(n) (Efficient with iterators)

Since LinkedList allows fast insertions and removals using iterators, it is an optimal choice.

Approach(using LinkedList)

Follow the below steps:

  • Store all elements in a LinkedList to preserve insertion order.
  • Insert elements into the list using the add(int x) method.
  • Traverse the list using an Iterator to safely remove all occurrences of the given element.
  • Print the remaining elements in the same order as they were inserted.

Output
20 30 
20 40 60 

Time Complexity

  • add(int x): O(1) -> inserting at the end of a LinkedList.
  • removeAndPrint(int x): O(n) -> traverses the list once to remove and print elements.

Space Complexity: O(n) -> extra space used to store n elements in the LinkedList.

Comment
Article Tags:
Article Tags: