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

Recommended Answers

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 …

Jump to Post

All 2 Replies

will you please post some working code over here.
so we can suggest some solution of your problem.

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.)