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 ; prlet the result ; Driver Code
function cntArray ( A , N ) { let result = 0 ; let frequency = Array . from ( { length : N + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { frequency [ A [ i ] ] ++ ; } for ( let i = 1 ; i <= N ; i ++ ) { let frequency_of_i = frequency [ i ] ; result += ( ( frequency_of_i ) * ( frequency_of_i + 1 ) ) / 2 ; } document . write ( result ) ; } let A = [ 1 , 5 , 6 , 1 , 9 , 5 , 8 , 10 , 8 , 9 ] ; let N = A . length ; cntArray ( A , N ) ;