I have two sets A and B.
A
--
1
2
6
B
--
1
2
3
4
When I compare set A with B, I need to get value 6 as output and value 4 as output when set B is compared against A.
I am wondering what would be the best algorithm to do this? I have wrote one but it has got a quadratic complexity. It basically iterate one set and inside the loop iterate the second set to check the value existence. I felt this as inefficient.
Context
I have a set of values in the database which I am showing in the UI. Users can remove or add new items to the list and press "Save changes" button which will persist all the changes to database. So here I need to insert newly added items to the database and delete removed items.
So I pass the first set which will have items that are newly added and already existing. I load another set which will have all the items from database. Now if I apply the above algorithm to compare Set A (new list) with Set B (database list) and take items that exist in SetA and not in SetB, I get all the newly added items. SetB will be then compared against SetA and all the items that exist in setB and not exist in SetA will be the deleted ones. I am open to suggestions for a better algorithm.
Any help would be great.