JavaScript statistics, removing duplicate characters

Source: Internet
Author: User

/**//*
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.