Fix performance issue for union method

In Object-Oriented Sets Assignment:
Fix performance issue for union method
Restructure code to remove isInstanceOf usage