I am trying to use javascript to remove duplicate entries in an array of arrays in which the duplicates will always be in an array of length 1. An example would be
[ [23, 46, 43, 44]
[46]
[52, 51]
[53]
[44]
[55, 66] ]
In this case I would want to remove the arrays [46] and [44], but leave [53]. The number of these single entry duplicate arrays is variable. Ideas?
Edit: Here's what I ended up doing that worked where stacks_array is the array of arrays.
function stacks_sanatizer(stacks_array){
var output_array = [],
used_cards =[];
used_cards[70] = undefined;
for (each_stak in stacks_array){
if((stacks_array[each_stak].length > 1) && (stacks_array[each_stak] !== "")){
for (i=0; i<stacks_array[each_stak].length;i++){
var card_number = stacks_array[each_stak][i].attrs.fill.substr(27).replace('.jpg)','');
used_cards[card_number - 1] = true;
}
}
}
for (each_stak in stacks_array){
if(stacks_array[each_stak] !== ""){
if(stacks_array[each_stak].length === 1){
card_number = stacks_array[each_stak][0].attrs.fill.substr(27).replace('.jpg)','');
if(used_cards[card_number - 1] !== true){
output_array.push(stacks_array[each_stak]);
used_cards[card_number - 1] = true;
}
}
else{
output_array.push(stacks_array[each_stak]);
}
}
}
//delete doubles
for(each_pile in output_array){
output_array[each_pile] = output_array[each_pile].filter(function(elem, pos) {
return output_array[each_pile].indexOf(elem) == pos;
})
}
return output_array;
}
-
1hashtables...alway hashtables. Well like 99% of the time it's hashtables. In addition check if the element came from an array of length 1.Ben– Ben2014年07月11日 21:06:12 +00:00Commented Jul 11, 2014 at 21:06
-
Can the single array to remove come before the duplicate entry in the longer array?Teepeemm– Teepeemm2014年07月11日 21:20:44 +00:00Commented Jul 11, 2014 at 21:20
-
2@Teepeemm oh that's a good question. To modify my comment a bit, you could iterate through the outer list twice, hashing inner lists of len > 1 the first time, and on the second time around, check for dupesBen– Ben2014年07月11日 21:22:54 +00:00Commented Jul 11, 2014 at 21:22
-
2Please show what you've tried. We're not here to do your work for you, we'll help you fix problems in your code.Barmar– Barmar2014年07月11日 21:27:03 +00:00Commented Jul 11, 2014 at 21:27
2 Answers 2
Pseudocode to accomplish this:
- Iterate through the outer array: for each inner array, hash the number into a hash table.
- While you hash it into the table, if there is an existing entry for it, check if the length of that array is 1, and if yes remove the array.
- Otherwise, enter the entry into the hash table and leave that inner array be.
Hope this helps :)
Comments
You would probably want to use some combination of a hash to lookup collisions and then splice what you don't want out.
Like so:
function removeDuplicates(listOfLists) {
var hash = {};
for (var i = 0; i < listOfLists.length; i++) {
var array = listOfLists[i];
for (var j = 0; j < array.length; j++) {
var val = array[j];
var hashedVal = hash[val];
if (hashedVal === undefined) {
hash[val] = true;
}
else {
array.splice(j, 1);
if (array.length === 0) {
listOfLists.splice(i, 1);
}
}
}
}
}