Java Program to Remove Duplicates from String
LinkedHashSet to keep characters unique and maintain order, like this: new LinkedHashSet<>(Arrays.asList(str.split(""))) and then join them back to a string.Examples
How to Think About It
Algorithm
Code
import java.util.LinkedHashSet; import java.util.Set; public class RemoveDuplicates { public static String removeDuplicates(String str) { Set<Character> seen = new LinkedHashSet<>(); for (char c : str.toCharArray()) { seen.add(c); } StringBuilder result = new StringBuilder(); for (char c : seen) { result.append(c); } return result.toString(); } public static void main(String[] args) { String input = "banana"; System.out.println(removeDuplicates(input)); } }
Dry Run
Let's trace the input "banana" through the code to see how duplicates are removed.
Initialize set and start loop
seen = {}, start looping over characters: 'b', 'a', 'n', 'a', 'n', 'a'
Add 'b'
seen = {'b'}
Add 'a'
seen = {'b', 'a'}
Add 'n'
seen = {'b', 'a', 'n'}
Skip duplicate 'a'
seen unchanged = {'b', 'a', 'n'}
Skip duplicate 'n'
seen unchanged = {'b', 'a', 'n'}
Skip duplicate 'a'
seen unchanged = {'b', 'a', 'n'}
Build result string
result = "ban"
| Iteration | Character | Set Contents | Result String |
|---|---|---|---|
| 1 | b | {b} | b |
| 2 | a | {b, a} | ba |
| 3 | n | {b, a, n} | ban |
| 4 | a | {b, a, n} | ban |
| 5 | n | {b, a, n} | ban |
| 6 | a | {b, a, n} | ban |
Why This Works
Step 1: Use LinkedHashSet to keep order
A LinkedHashSet stores characters uniquely and remembers the order they were added, so duplicates are removed but order stays the same.
Step 2: Add characters one by one
Loop through each character and add it to the set; duplicates are ignored automatically.
Step 3: Build the final string
After collecting unique characters, join them back into a string to get the result without duplicates.
Alternative Approaches
public static String removeDuplicatesAscii(String str) { boolean[] seen = new boolean[256]; StringBuilder result = new StringBuilder(); for (char c : str.toCharArray()) { if (!seen[c]) { seen[c] = true; result.append(c); } } return result.toString(); }
public static String removeDuplicatesStream(String str) { return str.chars() .distinct() .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) .toString(); }
Complexity: O(n) time, O(n) space
Time Complexity
The program loops through the string once, adding characters to a set and then building the result, so it runs in linear time relative to the string length.
Space Complexity
Extra space is used for the set and the result string, both proportional to the number of unique characters, which can be up to the string length.
Which Approach is Fastest?
Using a boolean array is fastest for ASCII but limited; LinkedHashSet is flexible and preserves order; streams offer concise code but may have slight overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| LinkedHashSet | O(n) | O(n) | Preserving order with any characters |
| Boolean array | O(n) | O(1) | Fast for ASCII characters only |
| Java Streams | O(n) | O(n) | Concise modern code, readable |