remove duplicate words from string in java

import java.util.*; import java.lang.*; class Demo{ public static void primary(String[] args){ String sen = “Sam went went to to to his enterprise”; String[] arr = sen.cut up(” “); //arr={Sam,went,went,to,to ,to ,his ,enterprise}; Set<String> s = new LinkedHashSet<String>(); for(int i=0;i<arr.size;i++){ s.add(arr[i]); } for(String ss:s){ System.out.print(ss+” “); } System.out.println(); } } Enter fullscreen mode Exit fullscreen mode