How to remove duplicate text in string.

Joined
Apr 17, 2018
Messages
1
Reaction score
0
Hi all,
i am new to Java...could you please help on this.
Scenario :
String Subject = RE:: This is test1 RE:: this is test2 RE:: this is test3 FW:: This is test1 FW:: this is test2 FW:: this is test3..

In Above Subject , RE & FW is repeating multiple times, i need to remove duplicate for both 'RE' & 'FW'..
so final o/p required as
String Subject = RE:: This is test1 this is test2 this is test3 FW:: This is test1 this is test2 this is test3.

how can we achive on this....
Thanks
Ram
 
Joined
Apr 25, 2017
Messages
247
Reaction score
31
Code:
public static void main(String[] args) {
    String stringWithDuplicates = "afsjeadrffafvgdefeverhfgberAAad";
    char[] characters = stringWithDuplicates.toCharArray();
    boolean[] found = new boolean[256];
    StringBuilder sb = new StringBuilder();
    System.out.println("String with duplicates : " + stringWithDuplicates);
    for (char c : characters) {
        if (!found[c]) {
            found[c] = true;
            sb.append(c);
        }
    }
    System.out.println("String after duplicates removed : " + sb.toString());
    }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top