DEV Community

realNameHidden
realNameHidden

Posted on

remove duplicate words from string in java



import java.util.*;
import java.lang.*;
class Demo{
    public static void main(String[] args){
        String sen = "Sam went went to to to his business";
        String[] arr = sen.split(" ");
        //arr={Sam,went,went,to,to ,to ,his ,business};
        Set<String> s = new LinkedHashSet<String>();
        for(int i=0;i<arr.length;i++){
            s.add(arr[i]);
        }
        for(String ss:s){
            System.out.print(ss+" ");
        }
        System.out.println();
    }   

}


Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)