admin管理员组文章数量:1414614
I want to implement a function to check if a given number do contain les than two different digits, that are called duodigits
For example :
12 , 110 , -33333 : are all duodigits , since they have no more than two different digits 102 : is not a duodigit since his digits ; 1 and 0 and 2 are three different digits
How may I implement a method , which may iterate and check if it's a duodgits or not and return true or false as result
I want to implement a function to check if a given number do contain les than two different digits, that are called duodigits
For example :
12 , 110 , -33333 : are all duodigits , since they have no more than two different digits 102 : is not a duodigit since his digits ; 1 and 0 and 2 are three different digits
How may I implement a method , which may iterate and check if it's a duodgits or not and return true or false as result
Share Improve this question asked Mar 25, 2022 at 22:17 AxelAxel 981 silver badge10 bronze badges 4-
Make a
Set
whose elements are all the digits in the number. Then get the size of the set. – Barmar Commented Mar 25, 2022 at 22:20 -
new Set(Math.abs(num).toString().split("")).length > 2
– 2pichar Commented Mar 25, 2022 at 22:24 -
@2pichar A
Set
doesn’t have alength
. You meansize
. – Sebastian Simon Commented Mar 25, 2022 at 22:27 -
@2pichar, note that you don't need the
split
because JS already considers strings to be arrays of characters ^_^ – Ben Commented Mar 25, 2022 at 22:56
3 Answers
Reset to default 2Here is how I would do it:
/**
* Checks if a number is a duodigit.
* @param {number} num
* @returns {boolean}
*/
function isDuodigit(num) {
return new Set(Math.abs(num).toString()).size <= 2;
}
my solution would be:
function isDuoDigit(num) {
const numArr = Array.from(num.toString());
const uniqueNums = numArr.filter((num, index) => numArr.indexOf(num) === index);
return uniqueNums.length > 2 ? "Number is not DuoDigit" : "Number is DuoDigit!"
}
console.log(isDuoDigit(11111000000)); // Number is DuoDigit!
console.log(isDuoDigit(111155555500000)); // Number is NOT DuoDigit!
Convert a number to an array of strings ( 102 = ["1", "0", "2"] )
const numArr = Array.from(num.toString());
Get an array with unique values (without duplicates)
const uniqueNums = numArr.filter((num, index) => numArr.indexOf(num) === index);
We check if length of uniqueNums array is more then 2. If it's true, it means number is NOT DuoDigit and if length of array 2 or less, number is DuoDigit
return uniqueNums.length > 2 ? "Number is not DuoDigit" : "Number is DuoDigit!"
If you want to do it without a set or string conversion.
function isDuoDigit(num){
num = Math.abs(num);
let first = '', second = '';
while (num > 0) {
let digit = num % 10;
if(first === '')
first = digit
else if(second === '')
second = digit
else if(first !== digit && second !== digit)
return false;
num = Math.trunc(num / 10);
}
return true;
}
本文标签: javascriptFunction to check if a given number do contain les than two different digitsStack Overflow
版权声明:本文标题:javascript - Function to check if a given number do contain les than two different digits - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745153884a2645051.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论