题目描述:

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 (bandB)
"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

我的解答:

function duplicateCount(text){
//...
var flag=0;
var a=text.toLowerCase();
while(a.length>0){
var reg=a.substring(0,1); a=a.substring(1);
if(a.indexOf(reg)!=-1){
a=a.replace(new RegExp(reg,'g'),"");
flag+=1; }
}
return flag;
}

优秀回答:

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

//若text=“Abccdeda”

//.toLowerCase() 将text全变成小写  “abccdeda”

//.split('')  以‘’规则分割                     [‘a’, 'b','c','c','d','e','d','a']

//.sort() 排序                                    ['a','a','b','c','c','d','d','e']

//.join() 将数组中所有元素放在一起 'aabccdde'

//.match(/([^])\1+/g) 将符合正则的放在新数组中 ['aa','cc','dd']

/([^])\1+/g  g表示全局,/... /表示正则内容,[^]排除,\1+至少出现一次,,,返回全局中重复出现的

//.length 求数组的长度

(2)
function duplicateCount(text){
return text.toLowerCase().split('').filter(function(val, i, arr){
return arr.indexOf(val) !== i && arr.lastIndexOf(val) === i;
}).length;
}

//  例子“abccdeda”

//filter(function(currentValue,index,arr){return xxx;})  返回符合条件的数组

//currentValue,必选,当前元素的值;index,可选,当前元素的索引在;arr,可选,当前数组对象

//  返回数组为['c','d',‘a’]

本题遇到的问题:

1,string的方法和Array的方法搞混了。

  String Array
输出第一个/最后一个元素

substring(0,1);

substr(-1); slice(-1);

shift(); slice(0,1);

pop();

替换 replace(searchvalue,newvalue); splice(index,howmany,item1,...,itemX);
其他重要方法

concat();

match();

search();

split();

concat();

join();

reverse();

sort();

2, replace();

用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串.

var str="Mr Blue has a blue house and a blue car";
var n=str.replace(/blue/gi, "red");

正则表达式RegExp   重要以后补充学习内容

var patt=new RegExp(pattern,modifiers);

或更简单的方法

var patt=/pattern/modifiers;

例如:

a=a.replace(new RegExp(reg,'g'),"");

3,indexOf();

判断字符串中是否含有子字符串。等同于contains

if(str.indexOf("substr")!=-1)

codewars--js--counting duplicates的更多相关文章

  1. [CodeWars][JS]实现链式加法

    在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...

  2. [CodeWars][JS]实现大整数加法

    问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...

  3. [CodeWars][JS]如何判断给定的数字是否整数

    问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...

  4. how to remove duplicates of an array by using js reduce function

    how to remove duplicates of an array by using js reduce function ??? arr = ["a", ["b& ...

  5. codewars--js--vowels counting+js正则相关知识

    问题描述: Return the number (count) of vowels in the given string. We will consider a, e, i, o, and u as ...

  6. 几个比较”有意思“的JS脚本

    1.获取内网和公网真实IP地址(引用地址) <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...

  7. 全排列算法的JS实现

    问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...

  8. ES6 will change the way you write JS code.

    https://hacks.mozilla.org/2015/04/es6-in-depth-an-introduction/ Counting to 6 The previous editions ...

  9. javascript 手势缩放 旋转 拖动支持:hammer.js

    原文: https://cdn.rawgit.com/hammerjs/hammer.js/master/tests/manual/visual.html /*! Hammer.JS - v2.0.4 ...

  10. codewars 随手记

    1.ES6数组遍历语法糖=> 在C#Linq里曾经用过,因此也不是很陌生. var range = Array.apply(null, Array(x)).map((_, i) => ++ ...

随机推荐

  1. es8对object快速遍历的方法

    let grade = { 'lilei' : 96, 'han' : 99 } //遍历keys console.log(Object.keys(grade)) console.log(Object ...

  2. 调试 ambari-server 总结

    刚开始debug ambari-server的时候,很多逻辑都是第一次接触.其中有很多知识点还是记录一下的好,做个备忘.这些知识点对于自定义api的开发还是很有作用的. 1. api的子href的最后 ...

  3. Spring Cloud学习之-什么是Spring Cloud?

    SpringCloud 什么是微服务? 要想学习微服务,首先需要知道什么是微服务?为什么会有微服务?相信看完架构的发展史读者就会明白 架构发展史 单体应用架构 如图所示:将所有的模块,所有内容(页面. ...

  4. 【javaScript】js出现allocation size overflow以及字符串拼接优化

    字符串拼接长一点了,就出现了allocation size overflow异常! 先创建缓冲字符串数组,最后将数组转化为字符串 <script type="text/javascri ...

  5. [新详细]让Keil5续签到2032年的办法,不可商用

    # 使用方法和以前的版本一样,MDK 或者C51等均适用,供学习与参考.更多需要到这里购买→ → Keil官网:[http://www.keil.com/](http://www.keil.com/) ...

  6. Excel.Application class

    https://docs.microsoft.com/en-us/javascript/api/excel/excel.application?view=office-js Represents th ...

  7. Collections.synchronizedList 、CopyOnWriteArrayList、Vector介绍、源码浅析与性能对比

    ## ArrayList线程安全问题 众所周知,`ArrayList`不是线程安全的,在并发场景使用`ArrayList`可能会导致add内容为null,迭代时并发修改list内容抛`Concurre ...

  8. sas9.2 windows7系统 10年11月后 建立永久数据集时,提示:“用户没有与逻辑库相应的授权级别

    先把你这个逻辑库删掉,在桌面创立空的新文件夹,然后用这个新文件夹在sas里新建逻辑库,名字照旧,再把你要的数据放进空文件夹就好了

  9. Python PE8编程规范

    参考博客:https://blog.csdn.net/weixin_39723544/article/details/82144280 1.使用四个空格而不是tab进行缩进 2.默认使用utf-8编码 ...

  10. <状压DP>solution-POJ3311_Hie with the Pie

    Hie with the Pie Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers ...