- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Counting n digit Numbers with all unique digits in JavaScript
Problem
We are required to write a JavaScript function that takes in a number, let’s say num, as the only argument. The function should count all such numbers that have num digits and all of their digits are unique.
For example, if the input to the function is −
const num = 1;
Then the output should be −
const output = 10;
Output Explanation:
The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 all have 1 digit and all are unique.
Example
The code for this will be −
const num = 1; const uniqueDigits = (num = 1) => { const dp = [1, 10]; const sum = [1, 11]; for (let i = 2; i <= num; i++) { dp[i] = sum[i - 1] + (10 - i) * (dp[i - 1]); sum[i] = sum[i - 1] + dp[i]; }; return dp[num]; }; console.log(uniqueDigits(num)); console.log(uniqueDigits(2)); console.log(uniqueDigits(3));
Code Explanation:
We have here used Dynamic Programming to keep track of desired numbers.
Output
And the output in the console will be −
10 91 739
- Related Articles
- Print all numbers less than N with at-most 2 unique digits in C++
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Count Numbers with Unique Digits in C++
- Counting specific digits used in squares of numbers using JavaScript
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Print all n-digit strictly increasing numbers in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- N digit numbers divisible by 5 formed from the M digits in C++
- Counting prime numbers from 2 upto the number n JavaScript
- Counting unique elements in an array in JavaScript
- Compute sum of digits in all numbers from 1 to n
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Counting smaller numbers after corresponding numbers in JavaScript
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++

Advertisements