10 Replies Latest reply on Sep 15, 2010 11:52 PM by jd_ice

    Store duplicate Array values to another Array

    jd_ice Level 1

      Hi flex coders,

       

      As the title says it all, how will I do this in flex? Like for example I have the values 1,1,1,2,2,3. All the duplicated entries need not to be remove but will just be stored in another collection.

       

      Need some light. Please help!

       


      Best Regards!

        • 1. Re: Store duplicate Array values to another Array
          rootsounds Level 4

          Would a 2D array make sense?

          • 2. Re: Store duplicate Array values to another Array
            Arun Ganesh Level 3

            Try this,

             

            var originalArray:Array = [1,2,3,4,2,1,3,3]
            var dupArray:Array = originalArray.slice(0,originalArray.length);
            dupArray=fRemoveDup(dupArray);

             

            private function fRemoveDup(ac:Array) : Array
                        {
                            var i : int;
                            var j : int;
                            for (i = 0; i < ac.length - 1; i++)
                                for (j = i + 1; j < ac.length; j++)
                                    if (ac[i] === ac[j])
                                        ac.splice(j, 1);
                            return ac;
            }

             

            Kindly mark it, If this is helpful.

            • 3. Re: Store duplicate Array values to another Array
              jd_ice Level 1

              Yeah surely. Can you show me how to do it?

              • 5. Re: Store duplicate Array values to another Array
                jd_ice Level 1

                The way I want it is like this output

                 

                originalArray = new Array(1,1,1,2,2,3)

                 

                output:

                 

                outArr[0] = new Array(1,1,1)

                outArr[1] = new Array(2,2)

                outArr[2] = new Array(3)

                 

                like these ones..

                • 6. Re: Store duplicate Array values to another Array
                  jd_ice Level 1

                  Actually a 2D is what I have. Here's what I got so far.

                   

                  var arr:Array = arrGroup.source as Array;

                  var currentValue:*='';

                  var tempArray:Array;

                  var newArray:Array = new Array();

                  var num:uint = 0;

                  var current:*='';

                  arr.forEach(

                  function(item:*,index:uint,array:Array):void

                  {

                  if(currentValue != item["key"])

                  {

                  tempArray = new Array();

                  currentValue = item["key"];

                  }

                  current = currentValue;

                   

                  if(current == item["key"])

                  {

                  var obj:Object = new Object();

                  obj["label"]   = item["label"];

                  obj["key"] = item["key"];

                  tempArray.push(obj);

                  }

                   

                   

                  if(tempArray.length > 0){

                  newArray[num] = tempArray;

                  num++;

                  }

                  }

                  );

                   

                  The output is like this.

                   

                         outArr:Array = new Array();

                   

                  outArr[0] = new Array(1,1,1)

                  outArr[1] = new Array(1,1,1)

                  outArr[2] = new Array(1,1,1)

                  outArr[3] = new Array(2,2)

                  outArr[4] = new Array(2,2)

                  outArr[5] = new Array(3)

                   

                  Depending on the duplicate occurence it's also creating the same number of extractions.

                  So how would do this to look it more like this

                   

                  outArr[0] = new Array(1,1,1)

                  outArr[1] = new Array(2,2)

                  outArr[2] = new Array(3)

                  • 7. Re: Store duplicate Array values to another Array
                    rootsounds Level 4

                    What about creating an associative array where you have keys that give you values representing occurences?

                     

                    Example:

                     

                    myArray[2] = 3; //3 occurences of the key 2

                    myArray["dummy"] = 1; //1 occurence of the key "dummy"

                    • 8. Re: Store duplicate Array values to another Array
                      jd_ice Level 1

                      Please help me review my code to see possible insertions of your idea. Wew, stuck to this the for a couple of hours, I think I should grab a bite first. Will check on this later. Thanks anyways.

                      • 9. Re: Store duplicate Array values to another Array
                        Arun Ganesh Level 3

                        try this,

                         

                        var originalArray:Array = [1,2,3,4,2,1,3,3]
                                         var distinctArray:Array = originalArray.slice(0,originalArray.length);
                                         distinctArray=fRemoveDup(distinctArray);
                                         var dupCollection:Array=seperateDuplicates(originalArray,distinctArray);

                                     private function seperateDuplicates(orgArray:Array,distinctArray:Array):Array{
                                         var aCollection:Array=[];
                                         for (var i:int = 0; i < distinctArray.length; i++){
                                             var tArray:Array = [];
                                           for (var j:int = 0; j < orgArray.length; j++){
                                                   if(orgArray[j]==distinctArray[i])
                                                       tArray.push(orgArray[j]);                              
                                            }
                                            aCollection.push(tArray);
                                        }
                                         return aCollection;
                                     }
                                    private function fRemoveDup(arr:Array):Array{
                                         var dupArr:Array = new Array();
                                         arr.sort();
                                         for (var i:int = 0; i < arr.length; i++){
                                           if (arr[i] == arr[i + 1])  continue;                  
                                          dupArr[dupArr.length] = arr[i];
                                         }
                                         return(dupArr);
                                    }

                        1 person found this helpful
                        • 10. Re: Store duplicate Array values to another Array
                          jd_ice Level 1

                          I got it. What I miss is actually the removing of duplicates. Thanks for the help! Til next time!