codewars js|Count the number of Duplicates 计算重复的字母数量

Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

Example

"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice

 

给定一个字符串比如说是 aabbcaAb,忽略大小写,a和b重复,返回2,返回的是重复的字母个数,而不是重复次数

对于 aabbcaAb,

1、先把所有字母变成小写,toLowerCase()(string对象的属性)

2、把字符串转成数组并且按照字典顺序排序,.split().sort(),['a','a','a','a','b','b','b','c']

3、把得到的数组组合成字符串,join(''),'aaaabbbc'

4、用正则表达式匹配重复字母 返回 ['aaaa','bbb','c']

5、返回以上数组长度

 

我的代码:

function duplicateCount(text){
  //...
  var str = text.toLowerCase().split('').sort().join('');
  var arr= str.match(/(.)\1+/g);
  console.log(arr);
  if(arr==null) return 0;
  else return arr.length; 
}

大佬的代码:

function duplicateCount(text){
  return (text.toLowerCase().split('').sort().join('').match(/([^])\1+/g) || []).length;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值