0

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.

4
  • array = Stream.of(array).distinct().toArray(n -> new String[n]); Commented Mar 10, 2016 at 14:03
  • @forgivenson I have edited the title. Commented Mar 10, 2016 at 14:06
  • One issue you have is you are removing elements from the list while you are looping through it, without decrementing i or j. This means you will be skipping some of the items in the list. Commented Mar 10, 2016 at 14:09
  • Sort the list, then check for each element if the following element startsWith() the current one. If so, remove the current one. Commented Mar 10, 2016 at 14:22

1 Answer 1

2

Here's a way to achieve this with Java 8.

This solution is case-sensitive - it should be trivial to improve it to case-insensitive if so required.

// test list
List<String> duplicates = new ArrayList<>(
    Arrays.asList(
        new String[]{
            "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"
        }
    )
);
// manipulating to Set with collections stream API
Set<String> noDuplicates = duplicates
    .stream()
    .filter(
        (s) -> duplicates.stream()
        // negative filter from original list
        .noneMatch(
            // condition: longer string that starts with current string
            (ss) -> ss.length() > s.length() && ss.startsWith(s)
        )
    )
    .collect(Collectors.toSet());

System.out.println(noDuplicates);

Output

[hour experience, restaurant, guy, local, room, jungle beach, morning, little hot, 
pagoda small, peaceful environment, view, ocean, car, eatables water, 
small garden, walk]
Sign up to request clarification or add additional context in comments.

2 Comments

How would i approach this problem if I was using Java 1.6.
@AND uugh. Too lazy, sorry. Maybe someone else can tackle the pre-stream solution...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.