9

I have a two lists of objects and I would like to remove the instances from one list which is there in other list.

e.g. I have following two lists and suppose each letter represents the object.

List listA = {A, B, C , D, E, F, G, H , I , J}

List listB= {D, G, K, P, Z}

Now, clearly listB has D and G which are there on listA too so I want listA to be like this

listA = {A, B, C , E, F, H , I , J}

Can you guys please suggest what would be the solution for this with O(n) or less than O(n2).

I can iterate over both the lists and remove the duplicate instances by comparing but I want to have something more efficient.

4

4 に答える 4

3

You could add both list elements to a Set

To remove elements on one list from another, try listA.removeAll(listB);

于 2013-04-09T00:00:03.037 に答える