Count subarrays having total distinct elements same as original array |  ; Count distinct elements in whole array ; Reset the container by removing all elements ; Use sliding window concept to find count of subarrays having k distinct elements . ; If window size equals to array distinct element size , then update answer ; Decrease the frequency of previous element for next sliding window ; If frequency is zero then decrease the window size ; Driver method
/ *function countDistictSubarray(arr,n) { let vis = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) vis . set ( arr [ i ] , 1 ) ; let k = vis . size ; let vid = new Map ( ) ; let ans = 0 , right = 0 , window = 0 ; for ( let left = 0 ; left < n ; left ++ ) { while ( right < n && window < k ) { if ( vid . has ( arr [ right ] ) ) vid . set ( arr [ right ] , vid . get ( arr [ right ] ) + 1 ) ; else vid . set ( arr [ right ] , 1 ) ; if ( vid . get ( arr [ right ] ) == 1 ) window ++ ; right ++ ; } if ( window == k ) ans += ( n - right + 1 ) ; if ( vid . has ( arr [ left ] ) ) vid . set ( arr [ left ] , vid . get ( arr [ left ] ) - 1 ) ; if ( vid . get ( arr [ left ] ) == 0 ) -- window ; } return ans ; } let arr = [ 2 , 1 , 3 , 2 , 3 ] ; document . write ( countDistictSubarray ( arr , arr . length ) ) ;