0

I want to remove/ delete duplicate words. I tried this but it wont work. Does someone have a solution for this?


    public static void main(String[] args) {
        List<String> words = sw("This is is an example");
        System.out.println(words); // => [an, example, is, This]
    }
    public static List<String> sw(String s) {
    List<String> words = new ArrayList<String>(Arrays.asList(s.trim().split(" +")));
    int size = words.size();
        for(int i = 0;i < size;i++) {
            for(int k = i+1;k < size;k++) { 
                if(size == 1) {
                    break;
                }
                if(k < size -1 && words.get(i).equals(words.get(k))) {  
                    words.remove(k);
                    k = k -1;
            }   if(size == 1) {
                break;
            }
                
         }
    }
        Collections.sort(words);
        return words;
}
}
dusty914
  • 11
  • 3
  • you need a set to keep unique values and then sort it. You can use sortedset implementation in java like TreeSet. – user1583465 Jan 23 '22 at 14:09
  • https://stackoverflow.com/questions/17967114/how-to-efficiently-remove-duplicates-from-an-array-without-using-set there are stackoverflow question – sawan Jan 23 '22 at 14:10

2 Answers2

0
you can solve this problem using LinkedHashSet or Regex.
1. LinkedHashSet
public static void main(String[] args) {
        String sentence, result = "";
        String allWords[];
        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your sentence: "); 
        sentence = sc.nextLine().toLowerCase();  //convert to lower case
        
        allWords = sentence.split(" ");
        
        LinkedHashSet<String> set = new LinkedHashSet<String>( Arrays.asList(allWords) );
        
        for(String word: set) {
            result = result + word + " ";
        }
        System.out.println("Sentence after removing duplicate words: " + result);

    }

2.Regex
Required regex = "\\b(\\w+)(?:\\W+\\1\\b)+";

public static void main(String[] args) {
        String sentence, regex;
        
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter your sentence: "); 
        sentence = sc.nextLine();
        // Define regex 
        regex = "\\b(\\w+)(?:\\W+\\1\\b)+";
        // Define pattern to compile regex
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        // Match whether regex matching with sentence or not
        Matcher match = pattern.matcher(sentence);
        // Use while loop to find and replace duplicate words
        while(match.find()) {
            sentence = sentence.replace(match.group(), match.group(1));
        }
        System.out.println("Sentence after removing duplicate words: " + sentence);

    }
-1

this should be the solution :)

public static List<String> sw(String s) {
            List<String> words = new ArrayList<String>(Arrays.asList(s.trim().split(" +")));
            int size = words.size();
                for(int i = 0;i < size;i++) {
                    for(int k = i+1;k < size;k++) { 
                        if(k <= size -1 && words.get(i).equals(words.get(k))) {  
                        words.remove(k);
                        k--;
                        size--;
                    }   
                }
            }
            Collections.sort(words);
            return words;
        }
    }
destroy_mo
  • 14
  • 3