codewars--js--counting duplicates
题目描述:
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的更多相关文章
- [CodeWars][JS]实现链式加法
在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...
- [CodeWars][JS]实现大整数加法
问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...
- [CodeWars][JS]如何判断给定的数字是否整数
问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...
- 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& ...
- 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 ...
- 几个比较”有意思“的JS脚本
1.获取内网和公网真实IP地址(引用地址) <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...
- 全排列算法的JS实现
问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...
- 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 ...
- javascript 手势缩放 旋转 拖动支持:hammer.js
原文: https://cdn.rawgit.com/hammerjs/hammer.js/master/tests/manual/visual.html /*! Hammer.JS - v2.0.4 ...
- codewars 随手记
1.ES6数组遍历语法糖=> 在C#Linq里曾经用过,因此也不是很陌生. var range = Array.apply(null, Array(x)).map((_, i) => ++ ...
随机推荐
- PythonI/O进阶学习笔记_11.python的多进程
content: 1. 为什么要多进程编程?和多线程有什么区别? 2. python 多进程编程 3. 进程间通信 ======================================= ...
- es7中数组如何判断元素是否存在
const arr = [1,2,3,4,5,6] console.log(arr.includes(4)) //true
- RocketMQ 解决 No route info of this topic 异常步骤
原文地址:https://blog.csdn.net/chenaima1314/article/details/79403113 rocketmq运行时提示 No route info of this ...
- umake ide -h
umake ide -husage: umake ide [-h] {netbeans,idea,clion,eclipse,atom,idea-ultimate,ec ...
- 迈向Angular 2
目录 序言 XV第1章 Angular 2快速上手 1Web的进化——新框架时代 2ECMAScript的进化 2Web Component 3WebWorker 4从AngularJS 1.x中学到 ...
- Frameworks.Entity.Core 5 EntityValidation
Project.Core\Frameworks.Entity.Core\EntityValidation\ EntityValidation 1 数值验证DigitAttribute.cs Digi ...
- 《代码整洁之道》&《程序员的职业素养》
这是why技术的第32篇原创文章 春节期间读了两本技术相关的书籍:编程大师Bob大叔的<代码整洁之道>和<代码整洁之道:程序员的职业素养>. <代码整洁之道>出版于 ...
- jsp路径
访问静态资源的时候${pageContext.request.Context}没有作用,在浏览器F12调试的时候发现,路径并没有被解释为项目的根路径,而是没有解释出来,还是${pageContext. ...
- vue-父组件向子组件传值
一.父组件向子组件传值 其实该问题是说子组件如何访问父组件的属性和方法?那么根据对组件化的理解,无非就是要解决两个问题: 1.父组件如何将值传给子组件? 2.子组件如何获取父组件传递过来的值? 解读v ...
- *args 和 **kwargs 的区别
截取百度里的两个答案: 这是Python函数可变参数 args及kwargs *args表示任何多个无名参数,它是一个tuple **kwargs表示关键字参数,它是一个dict 测试代码如下: de ...