Optimization Note. 

O(n^2) algorithm. Can be optimized with hashmap.