简体   繁体   English

如何计算数组中以最高编号开头的项目数?

[英]How do I count the number of items in an array that start with the highest number?

I've got an array that is filled with dates. 我有一个充满日期的数组。 They each start with the number of the year. 它们每个都以年份开始。 How do I cound the number of items present in the array that start with the highest number? 如何计算数组中以最高编号开头的项目数?

My array in question: 我的问题数组:

 ["12-A*januari", "12-B*februari", "12-C*maart", "12-D*april", "12-E*mei", "12-F*juni", "12-G*juli", "12-H*augustus", "12-I*september", "12-J*oktober", "12-K*november", "12-L*december", "13-A*januari", "13-B*februari", "13-C*maart", "13-D*april", "13-E*mei", "13-F*juni", "13-G*juli", "13-H*augustus", "13-I*september", "13-J*oktober", "13-K*november", "13-L*december", "14-A*januari", "14-B*februari", "14-C*maart", "14-D*april", "14-E*mei", "14-F*juni", "14-G*juli"]

So in this example I want to count the number of items in the array that start with 14, which in this example case would be 7. The numbers are dynamic so in the future the highest number will be different. 因此,在此示例中,我要计算以14开头的数组中的项目数,在本示例中为7。这些数字是动态的,因此将来最高的数字将有所不同。

How do i return the count for the items that start with the highest number? 如何返回以最高编号开头的项目的计数?

If your data is stored in arr : 如果您的数据存储在arr

var numbers = arr.map(function(x){ return parseInt(x); })
var max = Math.max.apply(Math,numbers)
var n = numbers.filter(function(x){ return x==max; }).length

Then your count will be in n . 然后,您的计数将为n

PS in your example array there are 7 elements starting with 14. 示例数组中的PS有7个元素,从14开始。

you can sort the array, reverse it, then just filter to get the count 您可以对数组进行排序,反转,然后进行过滤以获取计数

var count = ar.sort().reverse().filter(function(v){    
    return v.split('-')[0] == ar[0].split('-')[0];
}).length;

FIDDLE 小提琴

First loop through your array, splitting your string and finding the largest element, by looking at the first element of the new array, like this: 首先,通过查看新数组的第一个元素,遍历数组,拆分字符串并找到最大的元素,如下所示:

var splitString = iteratingString.split("-");
if(Number(splitString[0]) > max){
    max = Number(splitString[0]);
}

Then you iterate again, split each string again into a temporary array and increase the counter if the first element of the temp array is equal to max. 然后再次进行迭代,将每个字符串再次拆分为一个临时数组,如果temp数组的第一个元素等于max,则增加计数器。

Combinations of map/max/filter is elegant, but unless these functions were lazy, it's quite inefficient in theory since you have to loop multiple times over the input for each functionnal operation. map / max / filter的组合很优雅,但是除非这些函数是惰性的,否则在理论上效率很低,因为您必须为每个功能操作在输入上循环多次。

It really doesn't matter in practice, unless your input is very large, but why not something standard like the following? 在实践中,这实际上并不重要,除非您的输入非常大,但为什么不像下面这样的标准?

highestNumberOccurenceFrom(['13a', '12b', '13c']); //2

function highestNumberOccurenceFrom(arr) {
    var count = 0, max = 0, i = arr.length, num;

    while (i) {
        num = parseInt(arr[--i]);

        if (num > max) max = num, count = 1;
        else if (num === max) ++count;
    }

    return count;
}

You could try something like this: 您可以尝试这样的事情:

var maxCount = 0;
var maxVal = null;
var curVal = null;

for (var ctr=0; ctr< array.length; ctr++){
    //TODO: parse the number from the beginning of the string 
    //      You will need to write the function 'parseValFromString'
    curVal = parseValFromString(array[ctr]);
    if (curVal > maxVal || maxVal === null){
         maxVal = curVal;
         maxCount = 1;
    }
    else if (curVal ===  maxVal){
         maxCount++;
    }
}
console.log("count=" + maxCount;

Sort the Array first. 首先对数组进行排序。 Let the name of array be list 令数组名称为list

list.sort();

If you are sure that the array is already sorted, you can omit this step. 如果您确定数组已经排序,则可以省略此步骤。

Get the max number 获取最大数量

var max = list[list.length-1].substr(0,2);

Now filter the Array like this. 现在像这样过滤数组。

var list2 = list.filter(function(a){
    return max == a.substr(0,2);
})

Now get number of elements in list2. 现在获取list2中的元素数量。

var answer = list2.length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM