
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Checking for Pandigital Numbers in JavaScript
In this article, we will learn two approaches to check if a number is pandigital using JavaScript. Pandigital numbers are fascinating because they include each digit at least once within a specific range. Checking if a number is pandigital can be a common problem in programming challenges or number theory explorations.
What is a Pandigital Number?
A Pandigital number is a number that includes all the digits within a specific range at least once. For example ?
- A 0-9 pandigital number contains each digit from 0 to 9 at least once, regardless of order.
- Similarly, a 1-9 pandigital number would contain every digit from 1 to 9 without repetition.
1023456789 (0-9 pandigital)
987654321 (1-9 pandigital)
Using Arrays and Splice
In the first approach, we check if a number is pandigital by using a Legend Array that acts as a checklist.
Following are the steps to check for pandigital numbers ?
-
Legend Array: A legend array is initialized with digits from 0 to 9.
-
Iterating Through the Number String: Each digit in the input string is checked against the legend.
-
Removing Found Digits: If a digit exists in the legend, it is removed using a splice.
- Checking the Legend: If the legend array is empty after the loop, all digits from 0 to 9 are present.
Example
Below is an example to check for pandigital numbers using arrays and splice ?
const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => { let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; for(let i = 0; i < numStr.length; i++){ if(!legend.includes(numStr[i])){ continue; }; legend.splice(legend.indexOf(numStr[i]), 1); }; return !legend.length; }; console.log(isPandigital(numStr1)); console.log(isPandigital(numStr2));
Output
false true
Using Sets
A more efficient approach is to use a Set, which avoids frequent modifications and ensures uniqueness.
Following are the steps to check for pandigital numbers ?
- Set Initialization: A Set is created to hold unique digits from the input string.
- Comparison: Another Set is created for all digits from 0 to 9.
- Deleting Found Digits: Digits from the input are removed from the Set of all digits.
- Final Check: If the Set of all digits is empty, the number is pandigital.
Example
Below is an example to check for pandigital numbers using sets ?
const isPandigital = numStr => { const uniqueDigits = new Set(numStr); // Store unique digits const allDigits = new Set('0123456789'); // Set of all digits 0-9 for (let digit of uniqueDigits) { allDigits.delete(digit); // Remove each digit from the set } return allDigits.size === 0; // Check if all digits are found }; console.log(isPandigital('47458892414')); console.log(isPandigital('53657687691428890'));
Output
false true
Comparison Table
Feature | First Approach | Second Approach |
Ease of Understanding | Simple and beginner-friendly | Slightly complex |
Performance | Less efficient for large input | More efficient with Set |
Memory Usage | Requires maintaining legend |
Efficient use of Set |