modify O(nlogn) to O(n)