Jump to content

[SOLVED] Get all duplicate entries in array function.


mikesta707

Recommended Posts

Ok, I have a function, which I want to take all the entries that are duplicates in an array, and store them in another array. Right now It somewhat works, but not how I want it to. I want it to add every entry of the array (IE if there are 6 entries of two, it adds all 6 entries into the new array) Right now it only adds 1 entry that is duplicated. Here is the function as it stands

 

function getDuplicates(arr){
var r = new Array();
var keys = new Array();
one:for (var i = 0; i < arr.length; i++){
	for (var x = i + 1; x < arr.length; x++){
		if (arrayEqual(arr[x], arr[i])){
				keys.push(i)
		}
	}
}
for (var y = 0; y < keys.length; y++){
	r[y] = arr[keys[y]];
}
return r;
}

 

Note: This function is specific for 2 dimensional arrays. I need it this way.

 

Here is my test array:

    * [0] => object

                + [0] => taco

                + [1] => pig

                + [2] => crap

                + [3] => nonsense

    * [1] => object

                + [0] => dtaco

                + [1] => dpig

                + [2] => dcrap

                + [3] => dnonsense

    * [2] => object

                + [0] => taco

                + [1] => pig

                + [2] => crap

                + [3] => nonsense

    * [3] => object

                + [0] => dtaco

                + [1] => dpig

                + [2] => dcrap

                + [3] => dnonsense

    * [4] => object

                + [0] => adtaco

                + [1] => adspig

                + [2] => adcrap

                + [3] => nadonsense

 

As you can see, I want the new array to have 4 entries, basically the first 4 entries of my original test array (since 0 and 2, as well as 1 and 3 are both duplicates of each other). Right now, when I run the function, and do a print_r on it I get the following:

 

    * [0] => object

                + [0] => taco

                + [1] => pig

                + [2] => crap

                + [3] => nonsense

    * [1] => object

                + [0] => dtaco

                + [1] => dpig

                + [2] => dcrap

                + [3] => dnonsense

 

This result isn't terrible, as its kind of doing what I want, but I need it to add every entry that is a duplicate, not just one representative of the duplicates, if that makes sense. Anybody see where I am going wrong? BTW the arrayEqual function I could post, but it is not the problem (Well, I doubt it is, I have tested it quite thoroughly and it seems to work).

 

EDIT: sorry the arrays don't seem to like the way I copy pasted them... I hope you can read it

Link to comment
Share on other sites

nvm fixed it. If curious, it was very simple

 

function getDuplicates(arr){
var r = new Array();
var keys = new Array();
one:for (var i = 0; i < arr.length; i++){
	for (var x = i + 1; x < arr.length; x++){
		if (arrayEqual(arr[x], arr[i])){
			keys.push(x);
			keys.push(i);
		}
	}
}
for (var y = 0; y < keys.length; y++){
	r[y] = arr[keys[y]];
}
return r;
}

 

just had to push both of the equal keys, instead of only 1 of them!

 

Oh and if anyone is interested in the PHP print_r functions javascript equivalent, Its:

 

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

 

Taken from: http://www.brandnewbox.co.uk/articles/details/a_print_r_equivalent_for_javascript/

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.