0
0
JavaProgramBeginner · 2 min read

Java Program to Remove Duplicates from String

You can remove duplicates from a string in Java by using a 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

Inputhello
Outputhelo
Inputbanana
Outputban
Input
Output
🧠

How to Think About It

To remove duplicates from a string, think about checking each character one by one and keeping only the first time it appears. You can remember which characters you have seen using a set. Then, build a new string from these unique characters in the order they appeared.
📐

Algorithm

1
Get the input string.
2
Create an empty set to store seen characters.
3
Create a result string builder to collect unique characters.
4
For each character in the input string, check if it is in the set.
5
If not in the set, add it to the set and append to the result.
6
Return the result string.
💻

Code

java
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));
    }
}
Output
ban
🔍

Dry Run

Let's trace the input "banana" through the code to see how duplicates are removed.

1

Initialize set and start loop

seen = {}, start looping over characters: 'b', 'a', 'n', 'a', 'n', 'a'

2

Add 'b'

seen = {'b'}

3

Add 'a'

seen = {'b', 'a'}

4

Add 'n'

seen = {'b', 'a', 'n'}

5

Skip duplicate 'a'

seen unchanged = {'b', 'a', 'n'}

6

Skip duplicate 'n'

seen unchanged = {'b', 'a', 'n'}

7

Skip duplicate 'a'

seen unchanged = {'b', 'a', 'n'}

8

Build result string

result = "ban"

IterationCharacterSet ContentsResult String
1b{b}b
2a{b, a}ba
3n{b, a, n}ban
4a{b, a, n}ban
5n{b, a, n}ban
6a{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

Using boolean array for ASCII characters
java
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();
}
Faster for ASCII but only works for standard 256 characters and does not preserve order for extended Unicode.
Using Java 8 Streams
java
public static String removeDuplicatesStream(String str) {
    return str.chars()
              .distinct()
              .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
              .toString();
}
Modern and concise, uses streams to remove duplicates while preserving order.

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.

ApproachTimeSpaceBest For
LinkedHashSetO(n)O(n)Preserving order with any characters
Boolean arrayO(n)O(1)Fast for ASCII characters only
Java StreamsO(n)O(n)Concise modern code, readable
💡
Use a LinkedHashSet to remove duplicates while keeping the original order of characters.
⚠️
Beginners often try to remove duplicates by replacing characters without preserving order or using inefficient nested loops.