Sets can contain sObjects among other types of elements.
Sets contain unique elements. Uniqueness of sObjects is determined
by comparing the objects’ fields. For example, if you try to
add two accounts with the same name to a set, with no other fields
set, only one sObject is added to the set.
Account a1 = new account(name='MyAccount');
Account a2 = new account(name='MyAccount');
Set<Account> accountSet = new Set<Account>{a1, a2};
System.assertEquals(accountSet.size(), 1);
If you add a description to one of the accounts, it is considered
unique and both accounts are added to the set.
Account a1 = new account(name='MyAccount');
Account a2 = new account(name='MyAccount', description='My test account');
Set<Account> accountSet = new Set<Account>{a1, a2};
System.assertEquals(accountSet.size(), 2);