Actionscript:从数组中选择并显示重复的值
请问如何从数组中获取重复的值 例: 数组[10,3,10,10]Actionscript:从数组中选择并显示重复的值,actionscript,find,Actionscript,Find,请问如何从数组中获取重复的值 例: 数组[10,3,10,10] 显示位置1、3和4中有3个重复项 问候 我试过这个: q = new Array(); q = [A, B, C, D]; qValue = function (array) { equal = array[0]; for (j=0; j<array.length; j++) {
显示位置1、3和4中有3个重复项 问候 我试过这个:
q = new Array();
q = [A, B, C, D];
qValue = function (array) {
equal = array[0];
for (j=0; j<array.length; j++) {
if (array[j]===equal) {
equal = array[j];
}
}
return equal;
};
trace(qValue(q));
q=新数组();
q=[A,B,C,D];
qValue=函数(数组){
相等=数组[0];
对于(j=0;j类似的东西应该可以:
var array:Array = [1,2,3,4,3];
// create a dictionary and go through our array pulling out the keys
var dict:Dictionary = new Dictionary();
for each( var i:int in array )
{
if( i in dict ) // checks if the number is already in our dict as a key
dict[i]++;
else
dict[i] = 1;
}
// now go through our dictionary, finding the duplications
for( var key:* in dict )
{
var num:int = dict[key];
if( num == 1 )
continue; // single value - ignore
trace( "We have " + num + " occurrences of " + key );
}
编辑
要获得重复值的位置(标记),请改为使用:
var array:Array = [1,2,3,4,3];
// create a dictionary and go through our array, pulling out the values
var dict:Dictionary = new Dictionary();
var len:int = array.length;
for( var i:int = 0; i < len; i++ )
{
var val:int = array[i]; // get the value from the array
if( !( val in dict ) ) // if it's not in our dictionary, create a new array
dict[val] = [];
dict[val].push( i ); // add the index of the value to the array
}
// now go through our dictionary, finding the duplications
for( var key:* in dict )
{
var indicies:Array = dict[key];
if( indicies.length <= 1 )
continue; // single value - ignore
trace( "The value " + key + " is repeated " + indicies.length + " times. Indicies: " + indicies );
}
您的数组循环现在应该为
for( var i:int = 0; i < len; i++ )
{
var val:int = array[i]; // get the value from the array
var occurrenceArray = _getArray( obj, val ); // obj = dict
occurrenceArray.push( i ); // add the index of the value to the array
}
for(变量i:int=0;i
您尝试了什么吗?是的,请看第一篇博文谢谢,仍然是重复的位置的prb insee更新-我们只是用包含指示的数组替换计数它与as2不兼容:(将字典
替换为对象
,它应该仍然很好。如果(!(val in dict))
正在查看字典
/对象
中的键,则新编辑基本上都是,如果找到它,则返回true-您可以使用自己的函数来执行相同的操作
for( var i:int = 0; i < len; i++ )
{
var val:int = array[i]; // get the value from the array
var occurrenceArray = _getArray( obj, val ); // obj = dict
occurrenceArray.push( i ); // add the index of the value to the array
}