I know that something happen inside a computer while we practice coding in the leetcode and hacker rank but this explanation is next level. Thank you Alberto such a great explanation !!!
The problem with the first approach is it is giving you the expected value index but not which where it first appear so that's why we introducing the variable to keep the track of the index if duplicates exist!!