one pass solution