移出数组中重复的元素![抄自:蹩脚馒头]

2009-07-22 13:37:05

[code] var dc:Array = new Array(); dc.push(1); dc.push(1); dc.push(2); dc.push(&#34;a&#34;); dc.push(&#34;a&#34;); var obj:Object = {name:&#34;sshc625&#34;, sex:1}; dc.push(obj); dc.push(obj); var obj2:Object = {name:&#34;sshc625&#34;, sex:1}; dc.push(obj2); dc.push(obj2); // ------------------------------------------------------- var i:uint; trace(&#34;---------------->>before&#34;); for(i=0; i<dc.length; i++){ trace(i+&#34;--------&#34;+dc[i]); } dc = removeDuplicateElements( dc ); trace(&#34;---------------->>after&#34;); for(i=0; i<dc.length; i++){ trace(i+&#34;--------&#34;+dc[i]); } /** * Remove duplicate elements from dataCollection. * * @param dataCollection A data-collection which cantains duplicate elements. * * @return Array A data-collection which without any duplicate-elements. */ function removeDuplicateElements( dataCollection:Array ):Array{ if( dataCollection == null) return null; if( dataCollection.length <= 1) return dataCollection; var hash:Dictionary = new Dictionary( false ); var len:uint = dataCollection.length; for(var i:uint=0; i<len; i++) { hash[ dataCollection[i] ] = null; } dataCollection = new Array(); for(var pro:* in hash) { dataCollection.push( pro ); } return dataCollection; } [/code]