Sets can be compared against each other. Three methods let you combine two sets, keep only what they share, or strip out what they have in common.
addAll() pours every value of one set into another. Duplicates disappear on their own, because a set never stores a value twice.
Set<String> teamA = new Set<String>{ 'ana', 'ben' };
Set<String> teamB = new Set<String>{ 'ben', 'cora' };
teamA.addAll(teamB);
System.debug(teamA.size()); // 3, ben was already there
retainAll() keeps only the values that also live in the other set. Everything else is dropped.
Set<String> mine = new Set<String>{ 'apex', 'soql', 'lwc' };
Set<String> yours = new Set<String>{ 'soql', 'lwc', 'flow' };
mine.retainAll(yours);
System.debug(mine.size()); // 2, soql and lwc
removeAll() throws away every value that the other set also holds, leaving what is unique to the first set.
Set<Integer> codes = new Set<Integer>{ 100, 200, 300 };
codes.removeAll(new Set<Integer>{ 200 });
System.debug(codes.size()); // 2
All three methods change the set they are called on and return a Boolean, not a new set. If you still need the original values, copy the set first:
Set<String> shared = new Set<String>(mine); shared.retainAll(yours);
Now shared holds the overlap and mine is untouched.
Comparing two groups of ids is everyday work: which records did the query return that the input did not, which permissions does a user share with a profile, which emails appear in both files. These three methods answer those questions in one line.