The following is "empty thought one", not to Be continued ~ ~ ~ (Welcome comments, sincere and common progress with everyone!) )
(If you find an error, please correct me, I Xie)
there is a big difference between null and undefined, and it is OK to know. But learning lies not only in knowing, but in applying it to yourself. This article is only for "empty thought" introduction, will continue to update. Please look forward to! Have ideas, welcome to reply to the discussion. Can be in comment message interaction, can send email ([email protected])!
<script>
repeating characters and times in a "summary" string or array
Recommended method:
Regular mode: 1, the original string repeats the characters together, 2, the use of regular separate characters, 3, sorted by the number of repetitions.
General method: (principle is very important, deepen learning, exercise thinking)
//Duplicate Processing: Empty object Receive, For-if-else. The If branch judgment does not exist, is used to import and initialize (the first occurrence of the value, the empty object does not exist, the condition is executed to import and initialize); Else branch judgment exists, used to accumulate (after the iteration, repeat, to judge the existence, to perform the accumulation process).
//Review Result: Empty variable stored value, for-if. The traversal is found to match the condition (the number is initialized to 0. Can be understood as, the condition is initialized to the most non-conforming value, iteration variable, the condition should have the most value), iterate once, meet the condition to modify the value of the variable once, and then find in the judgment, until the match to find the condition of the save, the iteration continues, after not meet the conditions, is not modified.
var str = ' aaabbbcccaaabbbaaabbbbbbbbbb ';
0123456
var json = {};
for (var i = 0; i < str.length; i++) {
if (!json[str.charat (i)]) {//judgment exists for the first time i=0,json["a"] does not exist, json["a"]=1;
if (Json[str.charat (i)]===undefined) {//== does not error, but incorrect, non-canonical
Json[str.charat (i)] = 1;
}else{//i=6,json["A"] exists, json["a"]=1,1++=2,json["a"]=2;
Json[str.charat (i)]++;
}
};
var num = 0;//Maximum number of times value
var char = ';//Save the corresponding character key
for (Var key in JSON) {//var key in JSON
if (json[key]>num) {
Num= Json[key];
char = key;
}
}
Alert (' The most frequently occurring: ' +char+ ', appears ' +num+ ');
Console.log (JSON);//{a:9, s:7, D:1, f:1}
Console.log (json["a"]);//9
Console.log (' Most of the occurrences are: ' +char+ ', appearing ' +num+ ');//9
Recommended method:
Regular mode: 1, the original string repeats the characters together, 2, the use of regular separate characters; 3, sorted by repetition
var s =str;//s= ' aaabbbcccaaabbbaaabbbbbbbbbb ';
var a = S.split (");
Console.log (a);//Returns an array consisting of a single character in S
A.sort ();
s = A.join (");
Console.log (s);//Returns a string with the characters in array a sorted by ASC
The implementation of the original string sorted by ASC code, repeated together, the duplicate ASC code must be equal
var pattern =/(\w) \1*/g;//globally matches adjacent duplicates. Must be globally matched (no g, end of match).
var ans = s.match (pattern);
Console.log (ANS);
Ans.sort (function (A, B) {///comparator looks at the result of a A-B parameter, is in ascending order, negative is descending (no parentheses, the incoming sort is a callback function,use a function body as a parameter to pass in another function repeatedly; parentheses indicate the return value of only one function execution passed in.
return a.length < b.length;//length descending
//return b.length-a.length;//length descending
});
Console.log (ans);
Console.log (Ans[0][0] + ': ' + ans[0].length);
</SCRIPT>
" is looking at my blog of the children's shoes, I see you imposing, the conversation between the faint of the king of Qi, There will be a future! Here is a "top" word, you can just point it out!
of course, you can also "focus" and "collection", but I would like you to comment, interactive communication. We will discuss the study together, the efficiency is not higher? (to login to the blog Park account first) "
"Summary" "Empty thought one" string or repeated characters and times in an array