Count array elements whose all distinct digits appear in K | Java program to implement the above approach ; Function to the count of array elements whose distinct digits are a subset of the digits of K ; Stores distinct digits of K ; Iterate over all the digits of K ; Insert current digit into set ; Update K ; Stores the count of array elements whose distinct digits are a subset of the digits of K ; Traverse the array , arr [ ] ; Stores current element ; Check if all the digits of arr [ i ] are present in K or not ; Iterate over all the digits of arr [ i ] ; Stores current digit ; If digit not present in the set ; Update flag ; Update no ; If all the digits of arr [ i ] present in set ; Update count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int noOfValidKbers ( int K , int arr [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; while ( K != 0 ) { set . add ( K % 10 ) ; K = K / 10 ; } int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { int no = arr [ i ] ; boolean flag = true ; while ( no != 0 ) { int digit = no % 10 ; if ( ! set . contains ( digit ) ) { flag = false ; break ; } no = no / 10 ; } if ( flag == true ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int K = 12 ; int arr [ ] = { 1 , 12 , 1222 , 13 , 2 } ; System . out . println ( noOfValidKbers ( K , arr ) ) ; } }