Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Efficient removal of duplicates in unsorted linked list

$
0
0
I've written a method that accepts as a parameter an unsorted linked list and removes any duplicates found. The following is my code:

    public static<T> void remove(java.util.LinkedList<T> list)
    {       
        for(int i = 0; i < list.size(); i++)
        {
            for(int j = i + 1; j < list.size(); j++)
            {
                if(list.get(i).equals(list.get(j)))
                {
                    list.remove(j);
                }
            }
        }        
    }



Now, I'm wondering what is the more efficient way of doing this. If I'm not mistaken, this is O(n^2).

Thanks in advance!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>