package collectionPrograms;
public class RemoveDuplicatesWithoutHM {
public static void main(String[] args) {
// Declare all variables
String str, rs = "";
String Words[];
str ="I am I am a good girl when I am good" ;
str = str.toLowerCase(); //convert to lower case
// Split the given sentence to get each word as String array
Words = str.split(" ");
// Use for loop to remove duplicate words
for(int i=0; i<Words.length; i++) {
for(int j=i+1; j<Words.length; j++) {
if(Words[i].equals(Words[j])) {
Words[j] = "remove";
}
}
}
// Convert to String
for(String word: Words) {
if(word != "remove") {
rs = rs + word + " ";
}
}
// Display given String after removing duplicates
System.out.println("\nSentence after removing duplicate words: " + rs);
}
}
No comments:
Post a Comment