zoukankan      html  css  js  c++  java
  • js方式找出数组中重复数最多的那个数,并返回该数以及重复次数

    function findNum(a){
      var result = [0,0];
      for (var i = 0; i < a.length; i++) {
        for (var j = 0,count = 0; j < a.length; j++) {
          if (a[i]==a[j]) {
          ++count;
          };
        };
        if(count>result[0]) {
          result[0] = count;
          result[1] = a[i];
        }else if(count==result[0]&&result[1]<a[i]) {
          result[1] = a[i];
        }

      }
      alert("数"+result[1]+"重复了最多次,为"+result[0]);
    }
    var arr = [2,2,3,3,3,4,4,4,4,4,4,4,4,43,3];
    findNum(arr);

  • 相关阅读:
    isequal 和startswith 使用
    UVa10340
    UVa1368
    UVa455
    UVa1225
    UVa1586
    UVa 1585
    UVa10082
    UVa272
    NYOJ1
  • 原文地址:https://www.cnblogs.com/zhuzhenzhen/p/4597862.html
Copyright © 2011-2022 走看看