tags:

views:

217

answers:

3

Each set contains bunch of checksums. For example:
Set A:
{
4445968d0e100ad08323df8c895cea15
a67f8052594d6ba3f75502c0b91b868f
07736dde2f8484a4a3af463e05f039e3
5b1e374ff2ba949ab49870ca24d3163a
}

Set B:
{
6639e1da308fd7b04b7635a17450df7c
4445968d0e100ad08323df8c895cea15
a67f8052594d6ba3f75502c0b91b868f
}

The maximum common subset of A and B is:
{
4445968d0e100ad08323df8c895cea15
a67f8052594d6ba3f75502c0b91b868f
}

A lot of these operations will be performed, so I'm looking for an efficient algorithm to do so. Thanks for your help.

+4  A: 

Stick them in a hashtable and note the exact collisions.

Ignacio Vazquez-Abrams
+6  A: 

Put one of the sets in a hash table and iterate through the other, discarding elements that aren't in the hash. Alternatively, sort both and iterate through them simultaneously, as in merge sort.

EDIT: The latter method creates a sorted result. I should add that if the sets are of widely disparate sizes and they're presorted (say because you're doing a bunch of intersections), then you can realize a large performance improvement by using "unbounded" binary search to skip ahead in the large list.

+1  A: 
  1. Add Set A to a structure where you can find if a checksum exists.
  2. Loop Set B, check if element exists in Set A, if it exists, add to Set C

Set C is your common subset.

Carlos Gutiérrez