Arrays AS3查找数组中最常见的值
我想找到一种方法来查找数组中最常见的值,因此在名为“data”的bellow数组中,它有10个值为“1”。如果我发现很难找到这方面的任何信息,我将如何提取这些信息。任何帮助都将不胜感激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
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;
}
哇,太谢谢你了!!很好用。。很不错的!哇,太谢谢你了!!很好用。。很不错的!哇,太谢谢你了!!很好用。。很不错的!哇,太谢谢你了!!很好用。。很不错的!