How to remove duplicate text in string.

Discussion in 'Java' started by ramakrishnav, Apr 17, 2018.

  1. ramakrishnav

    ramakrishnav

    Joined:
    Apr 17, 2018
    Messages:
    1
    Likes Received:
    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
     
    ramakrishnav, Apr 17, 2018
    #1
    1. Advertisements

  2. ramakrishnav

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    72
    Likes Received:
    11
    Location:
    Malaysia
    Code (Text):
    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());
        }
     
    John Joe, Apr 17, 2018
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.