/**//*
Features: Statistics, removing duplicate characters
@param str needs a string of statistics
Description: Often used in string repeating characters, or in the array of repeated letters, numbers and so on count.
This collection of two typical types from the Internet, there are two ways to implement, there are many other variants, from different angles to write, searchable learning.
The data to be counted, both arrays and strings, can be used only with the help of String.Split () or Array.join ()
Convert to the type required by the function parameter.
*/
Type one: Save data with new object
| The code is as follows |
Copy Code |
| var count1 = function (str) { var map = {}, Maxcount = 0, Maxchar, undefined, i = str.length; while (i--) { var t = Str.charat (i); map[t] = = undefined? MAP[T] = 1:map[t] + = 1; if (map[t] > Maxcount) { & nbsp; Maxchar = t; maxcount = Map[maxchar]; } } return Character: "+ Maxchar +" times: + Maxcount; } function S_0 (a) {//where the parameter should be an array type var B = {}, c = [], I; for (i = 0; i < a.length; i++) { if (!b[a[i]]) { C[c.length] = A[i], b[a[i] = true; } } return C; } |
Type two: regular expression matching statistics
| code is as follows |
copy code |
| var Count2 = function (str) { var most = Str.split ('). Sort (). Join ('). Match (/(.) 1*/G); Arranges repeating characters most = Most.sort (function (A, b) {return a.length-b.length}). Pop (); p; return most.length + ': ' + most[0]; } Function S_1 (a) { var a = A.join (""), b = []; while (a.length > 0) a = A.replace (New RegExp (b[b.length] = A.charat (0)), "G"), ""); return B; } |