text: Count of pairs having each element equal to index of the other from an Array | Function to print the count of pair ; Iterate over all the elements of the array ; Increment the count ; Print the result ; Driver Code
code: function countPairs ( N , arr ) { let count = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( i == arr [ arr [ i ] - 1 ] - 1 ) { count ++ ; } } document . write ( count / 2 ) ; } let arr = [ 2 , 1 , 4 , 3 ] ; let N = arr . length ; countPairs ( N , arr ) ; let arr = [ 2 , 1 , 4 , 3 ] ; let N = arr . length ; countPairs ( N , arr ) ;