After adjusting the order of the characters with .split()
+ .sort()
+ .join()
could make a while
that will rotate from 0
up to the size of the string, picking up distinct characters. Inside the while
you use a regular expression that will select, at each loop of the loop, the repeated characters and count with .length
. If it’s bigger than 1
means that the character is repeated, and then the variable that controls the while
will be increased by the size .length
than the regular expression captured, otherwise it will be incremented normally with ++
.
For example, the string aabBcdeaa
will turn aaaabbcde
. On the first loop, regex will capture the 4 "a". Since 4 is greater than 1, it will increment the count on the variable count
with ++
and will also increment the variable i
with 4
(string size captured by regex). Then the i
(that initially had the value 0
) will become worth 4
(0+4
) and the next round of while
will now take the first character "b", and will do the same thing. A regex will take "bb", which has 2 of length
, and will add ++
in the variable count
and add +2
in the variable i
.
On the next turn you will start from the "c" character, and regex will return the size 1
, i.e., the "c" is unique, thus the variable count
will not be affected and the variable i
will be incremented only with ++
. That’s all I’m explaining you’ll see in the code below.
A regex text[i]+"+"
takes the respective character iterated by the loop while
in the string and group if there is more than 1 (sign of +
). For example, on the first round of while
, being the string aaaabbcde
, will return an array with the 4 "a":
["aaaa"]
In doing text.match(re)[0].length
will return the size of the string contained in the single item of the returned array (index [0]
), that is 4
.
Take the example:
function duplicateCount(text) {
text = text.toLowerCase().split('').sort().join('');
let count = i = 0;
while(i < text.length){
// escapar possíveis caracteres especias da regex
let str = text[i].replace(/[\.*+?^${}()|[\]]/g, '\\$&');
let len = text.match(str + "+")[0].length;
len > 1 ? (count++, i += len) : i++;
}
return count;
}
console.log(duplicateCount("aabBcdeaa")); // 2 -> a, b
console.log(duplicateCount("casa")) // 1 -> a
console.log(duplicateCount("c_asabfgbf_")) // 4 -> _, a, b, f
console.log(duplicateCount("13451090836")) // 3 -> 0, 1, 3
console.log(duplicateCount("aABCbc33zzzzzqw((")) // 6 -> 3, a, b, c, z, (
console.log(duplicateCount("-,-\^\^((")) // 3 -> -, ^, (
Notice that on the last console.log
the backslash is repeated 2 times (duplicateCount("-,-\^\^((")
) but it is not accounted for by being an escape character.
Another option using .indexOf()
and .lastIndexOf()
:
Like .indexOf()
returns the position of the first character found and .lastIndexOf()
the latter, if there is the same character more than once, the values will be different, but if the character is not repeated, the two methods will return the same value.
function duplicateCount(text) {
text = text.toLowerCase().split('').sort().join('');
let count = i = 0;
while(i < text.length){
let p1 = text.indexOf(text[i]);
let p2 = text.lastIndexOf(text[i]);
var len = text.substr(p1,p2-p1+1).length;
len != 1 ? (count++, i += len) : i++;
}
return count;
}
console.log(duplicateCount("aabBcdeaa")); // 2 -> a, b
console.log(duplicateCount("casa")) // 1 -> a
console.log(duplicateCount("c_asabfgbf_")) // 4 -> _, a, b, f
console.log(duplicateCount("13451090836")) // 3 -> 0, 1, 3
console.log(duplicateCount("aABCbc33zzzzzqw((")) // 6 -> 3, a, b, c, z, (
console.log(duplicateCount("-,-\^\^((")) // 3 -> -, ^, (
you want to take the most repeats and show the value?
– novic
@Virgilionovic no, I want to take the amount of elements that are repeated. Equal I put in the two examples.
– zangs
it’s very confusing the examples! can be clearer?
– novic
'Cause here it would have to be 2:
aabBcdeaa
it’s weird that– novic
@Virgilionovic take a good look at this string: "aabBcdeaa", the only elements that are repeated here are the 'a' and the 'b', that is, 2 repeated elements. Perhaps it will be clearer if I edit the question and explain that it is independent to be in upper or lower case.
– zangs
It is not clear because at the end of this text there are two more
aa
!?– novic
Buddy, it’s just any string, you know? It could be "home", for example. The result would be 1, in this case. Since the only character that repeats is 'a'. In the "aabBcdeaa" example, the only elements that repeat are 'b' and 'a', totaling 2 characters that are duplicated inside the string. I can’t be clearer than that.
– zangs