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) => ++ ...
随机推荐
- __call__ 方法
对象() 或 类()() 调用 __call__里面的方法 class Call: def __call__(self, *args, **kwargs): print("Hello __c ...
- 【红外DDE算法】聊聊红外图像增强算法的历史进程(第一回)
宽动态红外图像增强算法综述回顾过去带你回顾宽动态红外图像增强算法的历史进程,历来学者的一步步革命(新的算法框架提出),一步步改革(改进优化),从简单粗暴到细致全面.正所谓是:改革没有完成时,只有进行时 ...
- 【WPF学习】第十章 WPF布局示例
前几章用了相当大的篇幅研究有关WPF布局容器的复杂内容.在掌握了这些基础知识后,就可以研究几个完整的布局示例.通过研究完整的布局示例,可更好的理解各种WPF布局概念在实际窗口中的工作方式. 一.列设置 ...
- 域渗透之票据传递攻击(pass the ticket,ptt)
票据传递攻击(PtT)是一种使用Kerberos票据代替明文密码或NTLM哈希的方法.PtT最常见的用途可能是使用黄金票据和白银票据,通过PtT访问主机相当简单. 1.ptt攻击的部分 就不是简单的N ...
- c#数字图像处理(七)直方图匹配
直方图匹配,又称直方图规定化,即变换原图的直方图为规定的某种形式的直方图,从而使两幅图像具有类似的色调和反差.直方图匹配属于非线性点运算. 直方图规定化的原理:对两个直方图都做均衡化,变成相同的归一化 ...
- ReactNative---setState与性能的平衡
setState用来更新RN的视图层显示,每一次setState操作都会更新整个 视图,于是对应的是性能消耗,在某些特殊情况下就会造成卡顿 app假死等问题: 因此个人使用setState中总结的原则 ...
- maven远程部署到tomcat8服务器
maven远程部署到tomcat8服务器 环境准备 linux服务器一台 服务器安装JDK 服务器安装Tomcat 服务器Tomcat8配置 添加Tomcat权限 配置文件路径: tomcat/con ...
- TypeScript躬行记(6)——高级类型
本节将对TypeScript中类型的高级特性做详细讲解,包括交叉类型.类型别名.类型保护等. 一.交叉类型 交叉类型(Intersection Type)是将多个类型通过“&”符号合并成一个新 ...
- ProxySQL 基础篇
1.ProxySQL 介绍 ProxySQL 是基于 MySQL 的一款开源的中间件的产品,是一个灵活的 MySQL 代理层,可以实现读写分离,支持 Query 路由功能,支持动态指定某个 SQL 进 ...
- C++函数模板详解(一):概念和特性
函数模板是指这样的一类函数:可以用多种不同数据类型的参数进行调用,代表了一个函数家族.它的外表和普通的函数很相似,唯一的区别就是:函数中的有些元素是未确定的,这些元素将在使用的时候才被实例化.先来看一 ...