added method with time complexity o(n)