mikesta707 Posted August 25, 2009 Share Posted August 25, 2009 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 https://forums.phpfreaks.com/topic/171800-solved-get-all-duplicate-entries-in-array-function/ Share on other sites More sharing options...
mikesta707 Posted August 25, 2009 Author Share Posted August 25, 2009 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 https://forums.phpfreaks.com/topic/171800-solved-get-all-duplicate-entries-in-array-function/#findComment-905905 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.