As the default value of array is zero, we cannot distinguish default value and the last seen position if the last seen position is at 0. So, we should use one-based index instead of zero-based. As array uses zero-based index, we should use i+1 as the last seen position.
@eric108 The array sm and tm is the hash map. if sm[sc] == 0, then there is no key sc in the map. Since when you create the array sm,the value is assigned to 0. With sm[sc] = tc, we say that sc is the key and tc is the value.