Iterate for the first sub - array and store the maximum
for ( ; i < M ; i ++ ) { if ( m . ContainsKey ( A [ i ] ) ) { val = m [ A [ i ] ] ; m . Remove ( A [ i ] ) ; m . Add ( A [ i ] , val + 1 ) ; } else { m . Add ( A [ i ] , 1 ) ; } val = Math . Max ( val , m [ A [ i ] ] ) ; }