
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
Check for 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
Advertisements