I have an array list containing the following data;
[pagoda, hour, hour experience, pagoda, car, eatables, eatables water, walk, small, small garden, little, little hot, jungle, jungle beach, restaurant, ocean, local, room, morning, guy, pagoda small, small garden, hour experience, peaceful environment, view]
I want to check if an element in the arraylist contains value of another element, and if so remove the element which has the shorter length.
Eg:- look at hour, and hour experience in the arraylist above. I would need the method to remove hour element and keep only hour experience element.
This is what I wrote. But it doesn't work that well.
public void removeDuplicateKeywords(ArrayList<String> list){
System.out.println(list);
for(int i=0; i<list.size(); i++){
String keyword = list.get(i);
for(int j=i+1; j<list.size(); j++){
if(keyword.equals(list.get(j))){
list.remove(j);
}
if(list.get(j).trim().contains(keyword.trim()) && list.get(j).length() > keyword.length()){
list.remove(i);
}
}
}
System.out.println(list);
}
This will print
[pagoda, hour, hour experience, pagoda, car, eatables, eatables water, walk, small, small garden, little, little hot, jungle, jungle beach, restaurant, ocean, local, room, morning, guy, pagoda small, small garden, hour experience, peaceful environment, view]
[hour, hour experience, car, eatables water, walk, little, little hot, jungle beach, restaurant, ocean, local, room, morning, guy, pagoda small, small garden, peaceful environment, view]
Please help. Thanks in advance.