0

Currently this method has a run time of O(N^2) and I am trying to get it to O(N). Any suggestion as to where I should start?

    public void removeAll(e removeNum)
    {   
        node nodeToFind = this.find(removeNum);
        while(nodeToFind != null)
        {
            this.remove(nodeToFind);
            nodeToFind = this.find(removeNum);
        }
    }

Thanks

3
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by JamesCherrill
1

Details depend on what you are removing from (ArrayList, HashSet etc?), but re-starting the find from the beginning each time is what gives you the O(N^2).
Use an algorithm that just searches once through the data, removing as it goes. (You may need to start at the end of the data and iterate backwards towards the beginning, depending on what happens when you delete an item.)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.