runtime of O(N^2), need find O(N)