'Check if a number has repeated digits
How to check if a digit appears more than once in a number (anywhere within it)?
Example input numbers:
1, 10, 11, 1010, 1981
Output should tell which of them has any repeated digits:
false, false, true, true, true
Publihsed all the good answers given in a jsperf page
Solution 1:[1]
function checkForRepeatingDigits(N){
var arr = (''+N).split(''),
result = arr.filter((elem, i) => arr.indexOf(elem) == i);
return result.length != (''+N).length;
}
// Or
function checkForRepeatingDigits(N){
return [...new Set((''+N).split(''))].length != (''+N).length;
}
console.log([1, 10, 11, 1010, 1981].map(checkForRepeatingDigits))
Solution 2:[2]
You could use a check with Array#indexOf and Array#lastIndexOf.
function check(a, _, aa) {
return aa.indexOf(a) !== aa.lastIndexOf(a);
}
console.log([1, 10, 11, 1010, 1981].map(a => a.toString().split('').some(check)));
Solution 3:[3]
Short solution using Array.prototype.map() and String.prototype.match() function:
function checkForRepeatingDigits(N) {
return N.map(function (v) {
return [v, Boolean(String(v).match(/(\d)\d*?\1/g))];
});
}
console.log(checkForRepeatingDigits([1, 10, 11, 1010, 1981]));
Solution 4:[4]
function repeated(n) {
var digits = [];
var digit;
while (n) {
digit = n % 10;
if (digits[digit]) return true;
digits[digit] = true;
n = Math.floor(n / 10);
}
return false;
}
[1, 10, 11, 1010, 1981].forEach(n => console.log(n, repeated(n)));
Solution 5:[5]
This works by first converting the number to a string with N = N + '' and then checking the result of split(), which is a String function that crushes a string to smaller parts based on a delimiter.
For example, if I split "aba" by "b", I'll get an array containing ["a", "a"]. As you can see, if there's one occurrence of "b", the length of the returned array is 2. If there's more, it will be over 2. This is what I use in my solution.
As a bonus, it works with other types of data, even null and undefined. ;)
function check(N) {
for (var N = N + '', i = (N).length; i--;)
if (N.split(N[i]).length > 2)
return true;
return false;
}
[1, 10, 11, 1010, 1981, "abcd23", "aab", "", null, undefined].forEach(num => {
console.log(num, check(num));
});
Solution 6:[6]
const isRepeated = (num) => new Set('' + num).size != ~~Math.log10(num) + 1;
[1, 10, 11, 1010, 1981].forEach(n => console.log(n, isRepeated(n)));
JS offers a Set object which is a data type to hold unique elements, ie. it will remove any duplicates from the input, thus, new Set(string) removes all duplicate characters from the string, thus, we do new Set('' + num) to pass num as a string, now that the set contains only unique characters, set.size will return number of unique characters in the string.
A number will have no repeated characters if the number of digits is equal to number of unique characters we found earlier, there are many ways to find number of digits in number, we could also do num.toString().length instead of ~~Math.log10(num) + 1, the logarithm of a number to base 10 gives us a decimal value, we convert it to integer, this integer will be 1 less than the number of digits, so, we add 1 to get number of digits.
Thus, it returns true when the lengths are not equal, implying that there is repetition, and false otherwise.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Nina Scholz |
| Solution 3 | RomanPerekhrest |
| Solution 4 | |
| Solution 5 | |
| Solution 6 |
