title: Finding elements that are present in one set not the other
body: <p>I have two sets A and B. </p>
  
  <pre><code>A
  --
  1
  2
  6
  
  B
  --
  1
  2
  3
  4
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p><strong><em>Context</em></strong></p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>Any help would be great.</p>