Next Number with distinct digits | Javascript program to find next consecutive Number with all distinct digits ; Function to count distinct digits in a number ; To count the occurrence of digits in number from 0 to 9 ; Iterate over the digits of the number Flag those digits as found in the array ; Traverse the array arr and count the distinct digits in the array ; Function to return the total number of digits in the number ; Iterate over the digits of the number ; Function to return the next number with distinct digits ; Count the distinct digits in N + 1 ; Count the total number of digits in N + 1 ; Return the next consecutive number ; Increment Number by 1 ; Driver code
let INT_MAX = Number . MAX_VALUE ; function countDistinct ( n ) { let arr = new Array ( 10 ) ; arr . fill ( 0 ) ; let count = 0 ; while ( n != 0 ) { let r = n % 10 ; arr [ r ] = 1 ; n = parseInt ( n / 10 , 10 ) ; } for ( let i = 0 ; i < 10 ; i ++ ) { if ( arr [ i ] != 0 ) count ++ ; } return count ; } function countDigit ( n ) { let c = 0 ; while ( n != 0 ) { let r = n % 10 ; c ++ ; n = parseInt ( n / 10 , 10 ) ; } return c ; } function nextNumberDistinctDigit ( n ) { while ( n < INT_MAX ) { let distinct_digits = countDistinct ( n + 1 ) ; let total_digits = countDigit ( n + 1 ) ; if ( distinct_digits == total_digits ) { return n + 1 ; } else n ++ ; } return - 1 ; } let n = 2019 ; document . write ( nextNumberDistinctDigit ( n ) ) ;