1. Give you a string to find the most frequently-seen letters and the number of occurrences, for example, "abaasdffggghhjjkkgfddsssss ";
Copy codeThe Code is as follows:
Var str = "abaasdffggghhjjkkgfddsssss ";
Var arr = new Array ();
Var I = 0;
While (str. charAt (0 )){
Arr [I] = str. charAt (0) + "=" + (str. split (str. charAt (0). length-1 );
Str = str. split (str. charAt (0). join ("");
I ++;
}
Alert (arr );
For (var j = 0, temp = 0; j <arr. length; j ++ ){
If (temp <= Number (arr [j]. split ("=") [1]) {
Temp = Number (arr [j]. split ("=") [1]);
I = j;
}
}
Alert (arr [I]);
2. Solve the length of the string in bytes;
Copy codeThe Code is as follows:
Var f = function (s ){
If (! Arguments. length |! S ){
Return null;
}
If ("" = s ){
Return 0;
}
Var l = 0;
For (var I = 0; I <s. length; I ++ ){
If (s. charCodeAt (I)> 255 ){
L + = 2;
} Else {
L ++;
}
}
Alert (l );
};
F ("Hello ")
3. Remove repeated elements from the array;
Copy codeThe Code is as follows:
Array. prototype. strip = function (){
If (this. length <2 ){
Return [this [0] | [];
}
Var arr = [];
For (var I = 0; I <this. length; I ++ ){
Arr. push (this. splice (I --, 1 ));
For (var j = 0; j <this. length; j ++ ){
If (this [j] = arr [arr. length-1]) {
This. splice (j --, 1 );
}
}
}
Return arr;
};
Var a = ["abc", "abc", "a", "B", "c", "a", "B", "c"];
Alert (a. strip ());