Increment the count for the last visited element
int last_element = ( int ) A [ A . Count - 1 ] ; if ( mp . ContainsKey ( last_element ) ) mp [ last_element ] = mp [ last_element ] + 1 ; else mp [ last_element ] = 1 ; if ( mp [ last_element ] > maxVisited ) { maxVisited = mp [ last_element ] ; }