Remove duplicate values from a string in java

前端 未结 14 2596
刺人心
刺人心 2020-12-06 05:26

Can anyone please let me know how to remove duplicate values from

String s=\"Bangalore-Chennai-NewYork-Bangalore-Chennai\"; 

and output sh

14条回答
  •  情深已故
    2020-12-06 06:10

    Just the idea:

    1. parse the string and split the tokens using separator "-"
    2. load the tokens into a Collection
    3. iterate the Collection and erase duplicates
    4. use the result Collection to build the new string

    The most tricky part should be 3, but not impossible. If you use a Set, you can skip this step.

    EDIT maybe you can substitute 2&3 with a presence check before adding the element

提交回复
热议问题