In this article, we’ll explore two methods to remove duplicate elements from a stack in Java. We’ll compare a straightforward approach with nested loops and a more efficient method using a HashSet. The goal is to demonstrate how to optimize duplicate removal and to evaluate the performance of each approach.
Write a Java program to remove the duplicate element from the stack.
Input
Stackdata = initData(10L);
Output
Unique elements using Naive Approach: [1, 4, 3, 2, 8, 7, 5] Time spent for Naive Approach: 18200 nanoseconds Unique elements using Optimized Approach: [1, 4, 3, 2, 8, 7, 5] Time spent for Optimized Approach: 34800 nanoseconds
To remove duplicates from a given stack, we have 2 approaches −
Below is the Java program first builds a random stack and then creates a duplicate of it for further use −
private static Stack initData(Long size) { Stack stack = new Stack (); Random random = new Random(); int bound = (int) Math.ceil(size * 0.75); for (int i = 0; i manualCloneStack(Stack stack) { Stack newStack = new Stack (); for (Integer item: stack) { newStack.push(item); } return newStack; }
initData
helps create a Stack with a specified size and random elements ranging from 1 to 100.
manualCloneStack
helps generate data by copying data from another stack, using it for performance comparison between the two ideas.
Following are the step to remove duplicates from a given stack using Naïve approach −
Below is the Java program to remove duplicates from a given stack using Naïve approach −
public static Stack idea1(Stack stack) { long start = System.nanoTime(); Stack resultStack = new Stack (); while (!stack.isEmpty()) { int element = stack.pop(); if (!resultStack.contains(element)) { resultStack.add(element); } } System.out.println("Time spent for idea1 is %d nanosecond".formatted(System.nanoTime() - start)); return resultStack; }
For Naive approach, we use
while (!stack.isEmpty())
to handle the first loop to travel through all elements in the stack, and the second loop is
resultStack.contains(element)
to check if the element is already present.Following are the step to remove duplicates from a given stack using optimized approach −
Below is the Java program to remove duplicates from a given stack using HashSet −
public static Stackidea2(Stack stack) { long start = System.nanoTime(); Set seen = new HashSet(); Stack tempStack = new Stack(); while (!stack.isEmpty()) { int element = stack.pop(); if (!seen.contains(element)) { seen.add(element); tempStack.push(element); } } System.out.println("Time spent for idea2 is %d nanosecond".formatted(System.nanoTime() - start)); return tempStack; }
For optimized approach, we use
Set seen
to store unique elements in a Set, take advantage of O(1) complexity when accessing a random element to reduce the calculation cost of checking if an element is present or not.
Below are the step to remove duplicates from a given stack using both approaches mentioned above −
Below is the Java program that removes duplicate elements from a stack using both approaches mentioned above −
import java.util.HashSet; import java.util.Random; import java.util.Set; import java.util.Stack; public class DuplicateStackElements { private static StackinitData(Long size) { Stack stack = new Stack(); Random random = new Random(); int bound = (int) Math.ceil(size * 0.75); for (int i = 0; i cloneStack(Stack stack) { Stack newStack = new Stack(); newStack.addAll(stack); return newStack; } public static Stack idea1(Stack stack) { long start = System.nanoTime(); Stack resultStack = new Stack(); while (!stack.isEmpty()) { int element = stack.pop(); if (!resultStack.contains(element)) { resultStack.add(element); } } System.out.printf("Time spent for idea1 is %d nanoseconds%n", System.nanoTime() - start); return resultStack; } public static Stack idea2(Stack stack) { long start = System.nanoTime(); Set seen = new HashSet(); Stack tempStack = new Stack(); while (!stack.isEmpty()) { int element = stack.pop(); if (!seen.contains(element)) { seen.add(element); tempStack.push(element); } } System.out.printf("Time spent for idea2 is %d nanoseconds%n", System.nanoTime() - start); return tempStack; } public static void main(String[] args) { Stack data1 = initData(10L); Stack data2 = cloneStack(data1); System.out.println("Unique elements using idea1: " idea1(data1)); System.out.println("Unique elements using idea2: " idea2(data2)); } }
Output
* The unit of measurement is nanosecond.
public static void main(String[] args) { Stackdata1 = initData( ); Stack data2 = manualCloneStack(data1); idea1(data1); idea2(data2); }
Method | 100 elements | 1000 elements | 10000 elements |
100000 elements |
1000000 elements |
Idea 1 | 693100 |
4051600 |
19026900 |
114201800 |
1157256000 |
Idea 2 | 135800 |
681400 |
2717800 |
11489400 |
36456100 |
As observed, the time running for Idea 2 is shorter than for Idea 1 because the complexity of Idea 1 is O(n²), while the complexity of Idea 2 is O(n). So, when the number of stacks increases, the time spent on calculations also increases based on it.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3