

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 a Doubleton Number in JavaScript
Doubleton Number
We will call a natural number a "doubleton number" if it contains exactly two distinct digits. For example, 23, 35, 100, 12121 are doubleton numbers, and 123 and 9980 are not.
Problem
We are required to write a JavaScript function that takes in a number and return true if it is a doubleton number, false otherwise.
Example
Following is the code −
const num = 121212; const isDoubleTon = (num = 1) => { const str = String(num); const map = {}; for(let i = 0; i < str.length; i++){ const el = str[i]; if(!map.hasOwnProperty(el)){ map[el] = true; }; }; const props = Object.keys(map).length; return props === 2; }; console.log(isDoubleTon(num));
Output
Following is the console output −
true
- Related Questions & Answers
- JavaScript - Checking for pandigital numbers
- Checking for overlapping times JavaScript
- Checking for string anagrams JavaScript
- Checking for permutation of a palindrome in JavaScript
- Checking for uniqueness of a string in JavaScript
- Checking for Fibonacci numbers in JavaScript
- Checking for ascending arrays in JavaScript
- Checking for coprime numbers in JavaScript
- Checking for straight lines in JavaScript
- Checking for convex polygon in JavaScript
- Checking for increasing triplet in JavaScript
- Checking for special numbers in JavaScript
- Checking for co-prime numbers - JavaScript
- JavaScript Array: Checking for multiple values
- Checking for the Gapful numbers in JavaScript
Advertisements