Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays AS3查找数组中最常见的值_Arrays_Actionscript 3_Flash - Fatal编程技术网

Arrays AS3查找数组中最常见的值

Arrays AS3查找数组中最常见的值,arrays,actionscript-3,flash,Arrays,Actionscript 3,Flash,我想找到一种方法来查找数组中最常见的值,因此在名为“data”的bellow数组中,它有10个值为“1”。如果我发现很难找到这方面的任何信息,我将如何提取这些信息。任何帮助都将不胜感激 var data:Array = ["1","1","1","1","1","1","1","1","1","2","2","one","two","five","six","1","2","one","two","three","four","five","2","one","two","three","fou

我想找到一种方法来查找数组中最常见的值,因此在名为“data”的bellow数组中,它有10个值为“1”。如果我发现很难找到这方面的任何信息,我将如何提取这些信息。任何帮助都将不胜感激

var data:Array = ["1","1","1","1","1","1","1","1","1","2","2","one","two","five","six","1","2","one","two","three","four","five","2","one","two","three","four","five","2","five","2","one","two","five","six","2","one","two","five","six","2","one","two","five","six"];

results = "1";

这可能不是最有效的方法,但它确实起到了作用:

function mostCommonValue(array:Array):Object
{
    // create a dictionary of each unique item in the array and its count     
    var dict:Dictionary = new Dictionary(true);
    for each(var element:Object in array)
    {
        if(!dict[element]){
            dict[element] = 0;
        }
        dict[element]++;
    }

    var max:Number = 0;
    var mostCommon:Object;
    // loop over each item in the dictionary to find the highest number of occurrences 
    for(var key:Object in dict)
    {
        if(dict[key] > max){
            max = dict[key];
            mostCommon = key;
        }
    }

    return mostCommon;
}                

这可能不是最有效的方法,但它确实起到了作用:

function mostCommonValue(array:Array):Object
{
    // create a dictionary of each unique item in the array and its count     
    var dict:Dictionary = new Dictionary(true);
    for each(var element:Object in array)
    {
        if(!dict[element]){
            dict[element] = 0;
        }
        dict[element]++;
    }

    var max:Number = 0;
    var mostCommon:Object;
    // loop over each item in the dictionary to find the highest number of occurrences 
    for(var key:Object in dict)
    {
        if(dict[key] > max){
            max = dict[key];
            mostCommon = key;
        }
    }

    return mostCommon;
}                

这可能不是最有效的方法,但它确实起到了作用:

function mostCommonValue(array:Array):Object
{
    // create a dictionary of each unique item in the array and its count     
    var dict:Dictionary = new Dictionary(true);
    for each(var element:Object in array)
    {
        if(!dict[element]){
            dict[element] = 0;
        }
        dict[element]++;
    }

    var max:Number = 0;
    var mostCommon:Object;
    // loop over each item in the dictionary to find the highest number of occurrences 
    for(var key:Object in dict)
    {
        if(dict[key] > max){
            max = dict[key];
            mostCommon = key;
        }
    }

    return mostCommon;
}                

这可能不是最有效的方法,但它确实起到了作用:

function mostCommonValue(array:Array):Object
{
    // create a dictionary of each unique item in the array and its count     
    var dict:Dictionary = new Dictionary(true);
    for each(var element:Object in array)
    {
        if(!dict[element]){
            dict[element] = 0;
        }
        dict[element]++;
    }

    var max:Number = 0;
    var mostCommon:Object;
    // loop over each item in the dictionary to find the highest number of occurrences 
    for(var key:Object in dict)
    {
        if(dict[key] > max){
            max = dict[key];
            mostCommon = key;
        }
    }

    return mostCommon;
}                

哇,太谢谢你了!!很好用。。很不错的!哇,太谢谢你了!!很好用。。很不错的!哇,太谢谢你了!!很好用。。很不错的!哇,太谢谢你了!!很好用。。很不错的!