found o(n) algorithm