Mistakes i noted:
1. your union function is wrong. no items are ever added to result. an easy way to do it would be to have result contain the same contents of elements and then add items from s if...