题目描述:

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. css 脱离文档流

    一.float <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&quo ...

  2. GoldenGate DB11gr2配置手册

    GoldenGate DB11gr2配置手册 源端数据库配置 1.1源端数据库打开Archive Log: SQL>shutdown immediate; SQL>startup moun ...

  3. 每天玩转3分钟 MyBatis-Plus - 5. 高级查询(三)(条件构造器)

    每天玩转3分钟 MyBatis-Plus - 1. 配置环境 每天玩转3分钟 MyBatis-Plus - 2. 普通查询 每天玩转3分钟 MyBatis-Plus - 3. 高级查询(一) 每天玩转 ...

  4. Django 2.2

    Django 2.2 LTS 发布,长期支持版来了 django中文网:https://www.django.cn/course/course-3.html Django 2.2 已正式发布,这是一个 ...

  5. composer intall 报错

    报错 [Composer\Exception\NoSslException] The openssl extension is required for SSL/TLS protection but ...

  6. python(从放弃到从头开始)

    本节内容 Python介绍 发展史 Python 2 or 3? Hello World程序 变量 用户输入 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语句 表达式for ...

  7. obj2gltf安装详细教程

    在线转换地址:http://52.4.31.236/convertmodel.html 在使用cesium的过程中需要使用到gltf模型,官方推荐使用obj2gltf插件将obj模型转换成gltf格式 ...

  8. SpringCloud与微服务Ⅳ --- Rest微服务构建案例工程模块

    一.父工程搭建 父工程pom: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...

  9. Android教程2020 - RecyclerView显示多种item

    Android教程2020 - 系列总览 本文链接 前面我们已经用RecyclerView显示一些数据.也知道如何获取滑动的距离. 前面我们的列表中显示的都是同类数据.如果要在一个列表中显示不同类别的 ...

  10. js中函数this的指向

    this 在面试中,js指向也常常被问到,在开发过程中也是一个需要注意的问题,严格模式下的this指向undefined,这里就不讨论. 普通函数 记住一句话哪个对象调用函数,该函数的this就指向该 ...