0

I am having an issue with the following code. It is designed to remove duplicates (third and final entries) from an array of objects formatted as

{x: val, y: val2}

However, I will occasionally get outputs with duplicates (image) such as:

Object {x: 5, y: 0}
Object {x: 7, y: 0}
Object {x: 7, y: 5}
Object {x: 5, y: 2}
Object {x: 2, y: 1}
Object {x: 7, y: 5}

Why is this code not removing the duplicates as it is supposed to?

var mines = [{}];
console.log("\n\n\n\n\n");
for (var i = 0; i < 10; i++){ //for each mine that needs to be made
  var mine = {}; //initalize temporary object
  var dupeerr = false; //set duplication error flag to false 
                       //(will be flipped if loop needs to be re-executed)
  do{
    //generate coordinates for new mine
    //(random int between 0 and max size), then rounded to
    mine.x = Math.round(Math.random()*(8));
    mine.y = Math.round(Math.random()*(9));
    for (var j = mines.length - 1; j >= 0; j--) { //for each mine in array
      if ((mines[j].x == mine.x) && (mines[j].y == mine.y)) { //check for doubles
        dupeerr = true; //flag for re-execution
      } else if (mines[j] != mine) {
        dupeerr = false;
      }
    };
    //console.log(mines);
  } while (dupeerr || bounderr);

  mines[i] = mine;
}
var ans = [];

for (var i = 0; i < mines.length; i++) {
  ans[i] = mines[i];
  var minebtn = document.getElementById(JSON.stringify(ans[i]));
  console.log(ans[i]);
};
2
  • So the duplicate there is {x:7,y:5}? Commented Jun 19, 2013 at 1:38
  • is you question "why is this code not removing duplicates", or "how do I do what I want to do?" because if it's the latter, there's a nicely creative answer. Commented Jun 19, 2013 at 1:47

2 Answers 2

4

The problem is your for loop:

for (var j = mines.length - 1; j >= 0; j--) { //for each mine in array
  if ((mines[j].x == mine.x) && (mines[j].y == mine.y)) { // check for doubles
    dupeerr = true; //flag for re-execution
    break; // This should be sufficient to break you out of the for loop on true
  } else if (mines[j] != mine) {
    dupeerr = false;
  }
};

Even if it finds one it will continue on. On the next try it may be false and overwrite dupeerr to false.

Sign up to request clarification or add additional context in comments.

1 Comment

OP never resets the dupeerr flag in the 'do' loop on exit of the 'for' loop. So, you'd be right if the logic was shifted around a little. The comparison in the 'else if' block may be unnecessary.
1

You could do this with JSON. It's kind of hacky but it should work fine for simple objects:

var arr = [{x:1,y:1}, {x:1,y:2}, {x:1,y:1}, {x:1,y:2}];

function removeDups(arr) {
  return arr
    .map(JSON.stringify)
    .filter(function(v,i,self){ return self.indexOf(v) == i })
    .map(JSON.parse);
}

console.log(removeDups(arr)); //=> [{x:1,y:1}, {x:1,y:2}]

3 Comments

When a duplicate object is generated, it should be flagged in the inner loop, keeping it running until a unique one is made. Trouble is that OP is resetting the flag to false on the next iteration when a dupe is found.
...and FWIW, you can do .map(JSON.stringify) and .map(JSON.parse).
@CrazyTrain: Of course, silly me. Well I'll leave the question here if this helps OP even tho it doesn't really solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.