
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- Checking for uniqueness of a string in JavaScript
- Checking for permutation of a palindrome in JavaScript
- Checking for Fibonacci numbers in JavaScript
- Checking for coprime numbers in JavaScript
- Checking for ascending arrays in JavaScript
- Checking for straight lines in JavaScript
- Checking for convex polygon in JavaScript
- Checking for special numbers in JavaScript
- Checking for increasing triplet in JavaScript
- JavaScript - Checking for pandigital numbers
- Checking for overlapping times JavaScript
- Checking for string anagrams JavaScript
- Checking for the Gapful numbers in JavaScript
- Checking for centrally peaked arrays in JavaScript
- Checking for majority element in a sorted array in JavaScript

Advertisements