Assume I have a Student structure
struct Student
{
int id;
std::string name;
int age;
float dob; // date of birth in seconds, this is just for simplicity's sake
};
And I have two different arrays of Student
s (I open, read and parse two different text files)
Student arr1[FIRST_ARR_SIZE];
Student arr2[SECOND_ARR_SIZE];
now, my problem is that I want to compare the two arrays and every Student
object that exists in one array but doesn't exist in the other array should be stored in
Student output_arr1[]; // Student objects that only exist in arr1
Student output_arr2[]; // Student objects that only exist in arr2
Things to consider:
The only two solutions I could think of, off of the top of my head, are:
for (Student s : arr1) {
// do stuff
for (Student s : arr2) {
// do stuff
}
// do stuff
}
Obviously this is not good at all.
Or, a much better solution for large-ish (tens / hundreds of thousands) N and M (which is true in my case),
I was wondering if there were any clever algorithms to do this even quicker than those two?
Linear time, linear space:
Put everything from the smaller array in a hash.
Parse the large array. For each element in the large array:
At the end, everything remaining in the hash is only in the smaller array, so add those to the output as well.