Count array elements whose all distinct digits appear in K | Function to check a digit occurs in the digit of K or not ; Iterate over all possible digits of K ; If current digit equal to digit ; Update K ; Function to find the count of array elements whose distinct digits are a subset of digits of K ; Stores count of array elements whose distinct digits are subset of digits of K ; Traverse the array , [ ] arr ; Stores the current element ; Check if all the digits arr [ i ] is a subset of the digits of K or not ; Iterate over all possible digits of arr [ i ] ; Stores current digit ; If current digit does not appear in K ; Update flag ; Update no ; If all the digits arr [ i ] appear in K ; Update count ; Finally print count ; Driver Code
function isValidDigit ( digit , K ) { while ( K != 0 ) { if ( K % 10 == digit ) { return true ; } K = parseInt ( K / 10 ) ; } return false ; } function noOfValidNumbers ( K , arr , n ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { let no = arr [ i ] ; let flag = true ; while ( no != 0 ) { let digit = no % 10 ; if ( ! isValidDigit ( digit , K ) ) { flag = false ; break ; } no = parseInt ( no / 10 ) ; } if ( flag == true ) { count ++ ; } } return count ; } let K = 12 ; let arr = [ 1 , 12 , 1222 , 13 , 2 ] ; let n = arr . length ; document . write ( noOfValidNumbers ( K , arr , n ) ) ;