You have to specify in the loop that the element is found you can stop now, to solve the issue use break before incrementing the value of i