Search for the maximum number of occurrences of a character in a string, the most frequently occurring character, and the index of the character with the most occurrences

Source: Internet
Author: User

<! DOCTYPE html>

Css.css

div {    width:800px;    height:200px;    Background: #f1f1f1;    margin:0 Auto;    Text-align:center;    line-height:20px;    border:1px solid #ccc;    border-radius:10px;} h2 {    font-size:20px;    width:100%;} button {    cursor:pointer;    Background: #eee;    border:1px solid #ddd;} input {    width:165px;    height:25px;    border:1px solid #ddd;}

Js.js

Window.onload = function () {var obtn = document.getelementsbytagname ("button");    var oipt = document.getelementsbytagname ("input");        Obtn[0].onclick = function () {var str = oipt[0].value;        var Objchar = {};            for (var i = 0; i < str.length; i++) {var char = Str.charat (i);            if (Objchar[char]) {Objchar[char] + = 1;            } else {Objchar[char] = 1;        }} var maxCount = 0;        var maxcharindex = [];                for (var key in Objchar) {if (MaxCount < Objchar[key]) {maxCount = Objchar[key];                Maxcharindex = [];            Maxcharindex.push (key);            } else if (MaxCount = = Objchar[key]) {Maxcharindex.push (key);        }} function GetCount () {return maxCount;        } function Getmaxchar () {return maxcharindex;        } var objindex = {}; var maxindex= []; for (var j = 0; J < Maxcharindex.length, J + +) {for (var i = 0; i < str.length; i++) {if (Str[i] = = Maxcharindex[j])                {Maxindex.push (i);                    } if (i = = str.length-1) {Objindex[maxcharindex[j]] = Maxindex;                Maxindex = [];            }}} function GetIndex () {var maxIndex2 = [];            for (var key in Objindex) {Maxindex2.push (key +) is indexed ("+ Objindex[key] +") ";        } return MAXINDEX2;        } Oipt[1].value = GetCount ();        Oipt[2].value = Getmaxchar ();    Oipt[3].value = GetIndex (); }}

  

Search for the maximum number of occurrences of a character in a string, the most frequently occurring character, and the index of the character with the most occurrences

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.