Strings, arrays common examples

1: find the longest string of characters in a string

var st="The quick brown fox jumped bcaked jumped abscefg over the lazy dog 15625765675276";
     function zifuchuanmax(str){
         var arr=str.split(" ");//将字符串转化为数组
         var max=arr[0];
         for(var i=1;i<arr.length;i++){
             if(max.length<arr[i].length){
                 max=arr[i];
             }
         }
         return max;
     }
     console.log(zifuchuanmax(st));//15625765675276

Do problems: First method using a split string into an array, a variable is defined, the first element of the array is assigned to the variable, and then for the loop elements of the array are sequentially compared with the variable, if the variable ratio long, then the element assigned to the variable
2: weight to an array, the elements of an array of identical removed

var arr = [1, 1, 2, 3, 4, 3, 5, 5, 6, 7, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9];
     function szqc(array){
         for(var i=0;i<array.length;i++){
             var same=array[i];
             for(var k=i+1;k<array.length;k++){
                 if(same==array[k]){
                     array.splice(k,1);//删除元素之后,后面元素会自动补位,但下次循环系统会自动跳过该位置元素,所以补位元素就会被忽略
                     k--;//退回去会将补位元素不被忽略
                 }
             }
         }
         return array;
     }
     console.log(szqc(arr));// [1, 2, 3, 4, 5, 6, 7, 8, 9]

3: Flip string
Method a:

var str = "abcdefghi";
console.log(str.split("").reverse().join(""));//ihgfedcba

Convert a string array, then use of the array method of reverse inverted array, then use the join () which was converted to a string
Method two:

var str = "abcdefghi";
 function fanzhuan(str){
         var dao="";//定义一个空字符串
         for(var i=str.length-1;i>=0;i--){
             dao+=str.charAt(i);
         }
         return dao;
     }
     console.log(fanzhuan(str));//ihgfedcba

Defining a new function in the empty string, then use for loop, flashbacks remove the original string element superimposed
4:
Statistics and a highest number of occurrences of a given string appears in the string, the statistics appear most often letters. Such as: "cccccccccasdfssaaasasasasaadddddddd",

var str1 = "cccccccccasdfssaaasasasasaadddddddd";
function findChar(str) {
        var ss = moveNumber(str.split(""));//先利用数组去重,将每个元素取出
        /*[a,c,f,h]*/
        var list = [];
        for (var i = 0; i < ss.length; i++) {
                var count = 0;
                for (var index in str) {
                        if (ss[i] == str.charAt(index)) {
                                count++;
                        }//
                }
                list.push([ss[i], count]);
        }
        var tong = 1;
        for (var i = 0; i < list.length; i++) {
                if (list.length <= tong)break;
                if (list[i][1] > list[i + 1][1]) {
                        list.splice(i + 1, 1);
                        i--;
                }
                else if (list[i][1] < list[i + 1][1]) {
                        list.splice(i, 1);
                        i--;
                }
                else {
                        tong++;
                }
        }
        return function () {
                var strlist = "";
                for (var i = 0; i < list.length; i++) {
                        strlist += list[i][0] + "---" + list[i][1] + "个"
                }
                return strlist;
        }

}
console.log(findChar(str1)());//c---9个a---9个d---9个

Guess you like

Origin blog.51cto.com/14419253/2430278