0

For the exam, I train to write the some algorithms to the final exam. One of them is creating the reverse() method, which removes doubles in the List<> . The problem that the void method only removes one double or two. How can I change the method?

Here, the code of the List.java file. The methods removeFromBack(), removeFromFront(), insertAtBack(), insertAtFront(), print(), isEmpty(), the classes List and ListNode are already defined in Deitel's Java book. Additionally, all imports are done:

public void removeDuplicates() {
    ArrayList<T> toCheck = new ArrayList<T>();
    ListNode<T> current = firstNode;
    while (current != null) {
        toCheck.add(current.data);
        current = current.nextNode;
    }
    current = firstNode;
    HashSet<T> toCheck2 = new LinkedHashSet<T>();

    for (T element: toCheck) {
        toCheck2.add(element);
    }

    for (T element: toCheck2) {
        removeFromBack();
        insertAtBack(element);
    }
}

3 Answers 3

1

you can add duplicates list and remove them like this:

    public void removeDuplicates() {
        ArrayList<T> elements = new ArrayList<T>();
        ListNode<T> current = firstNode;
        while (current != null) {
            if(!elements.contains(current.data))
              toCheck.add(current.data);
            else {
               // you have the duplicates, do your logic
            }
            current = current.nextNode;
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but 1) better to add removeFromFront(); after else 2) not use removeFromBack(); 3) and T element : elements, not T element : duplicates. Others are right
0

There are faster/more efficient ways depending on your needs but this one should basically always work:

public static <T> ArrayList<T> removeDuplicates(ArrayList<T> toCheck) {

    ArrayList<Integer> toRemove = new ArrayList<Integer>();

    for(int i = 0; i < toCheck.size(); i++) {
        T current = toCheck.get(i);

        for(int j = 0; j < toCheck.size(); j++) {
            if(j == i)continue;

            if(toCheck.get(j) == current) {
                toRemove.add(j);
            }
        }
    }

    Collections.sort(toRemove, Collections.reverseOrder());

    for(int i : toRemove) {
        toCheck.remove(i);
    }

    return toCheck;

}

1 Comment

I know that it is possible to use either native methods or arguments, but no arguments are needed.
0

For duplicates in List, you could always use Set to help to remove the duplicates:


public List<Integer> removeDuplicates(List<Integer> list) {
    Set<Integer> set = new HashSet<>(list); // remove all duplicates in set
    List<Integer> result = new ArrayList(set.size());
    for (Integer i : list) {
         if (set.contains(i)) {
             result.add(i);
             set.remove(i); // delete, so duplicate item will not be added to result twice
         }
    }

    return result;
}

2 Comments

I knew, but the case that it should work with non-primitive types, which HashSet couldn't describe.
Set could work on objects, as long as you override the equals and hashcode method in this object.

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.