Count of subarrays of an Array having all unique digits | Function to check whether the subarray has all unique digits ; Storing all digits occurred ; Traversing all the numbers of v ; Storing all digits of v [ i ] ; Checking whether digits of v [ i ] have already occurred ; Inserting digits of v [ i ] in the set ; Function to count the number subarray with all digits unique ; Traverse through all the subarrays ; To store elements of this subarray ; Generate all subarray and store it in vector ; Check whether this subarray has all digits unique ; Increase the count ; Return the count ; Driver code
function check ( v ) { let digits = new Set ( ) ; for ( let i = 0 ; i < v . length ; i ++ ) { let d = new Set ( ) ; while ( v [ i ] ) { d . add ( v [ i ] % 10 ) ; v [ i ] = Math . floor ( v [ i ] / 10 ) ; } for ( let it of d ) { if ( digits . has ( it ) ) return false ; } for ( let it of d ) digits . add ( it ) ; } return true ; } function numberOfSubarrays ( a , n ) { let answer = 0 ; for ( let i = 1 ; i < ( 1 << n ) ; i ++ ) { let temp = new Array ( ) ; for ( let j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) temp . push ( a [ j ] ) ; } if ( check ( temp ) ) answer ++ ; } return answer ; } let N = 4 ; let A = [ 1 , 12 , 23 , 34 ] ; document . write ( numberOfSubarrays ( A , N ) ) ;