Count of subarrays which start and end with the same element | Function to find total sub - array which start and end with same element ; Initialize result with 0 ; Array to count frequency of 1 to N ; Update frequency of A [ i ] ; Update result with sub - array contributed by number i ; Print the result ; Driver code
def cntArray ( A , N ) : NEW_LINE INDENT result = 0 NEW_LINE frequency = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT frequency [ A [ i ] ] = frequency [ A [ i ] ] + 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT frequency_of_i = frequency [ i ] NEW_LINE result = result + ( ( frequency_of_i ) * ( frequency_of_i + 1 ) ) / 2 NEW_LINE DEDENT print ( int ( result ) ) NEW_LINE print ( " " ) NEW_LINE DEDENT A = [ 1 , 5 , 6 , 1 , 9 , 5 , 8 , 10 , 8 , 9 ] NEW_LINE N = len ( A ) NEW_LINE cntArray ( A , N ) NEW_LINE