Print a number strictly less than a given number such that all its digits are distinct . | Function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ; creating a copy of i ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; Driver code
function findNumber ( n ) { for ( i = n - 1 ; i >= 0 ; i -- ) { var count = Array . from ( { length : 10 } , ( _ , i ) => 0 ) ; var x = i ; var count1 = 0 , count2 = 0 ; while ( x > 0 ) { count [ x % 10 ] ++ ; x = parseInt ( x / 10 ) ; count1 ++ ; } for ( j = 0 ; j < 10 ; j ++ ) { if ( count [ j ] == 1 ) count2 ++ ; } if ( count1 == count2 ) return i ; } return - 1 ; } var n = 8490 ; document . write ( findNumber ( n ) ) ;