# Mutations - How to get rid of duplicate elements in an array

Mutations - How to get rid of duplicate elements in an array
0
#1

Hello! I am working on the freeCodeCamp challenge “Mutations”, and I’m not sure how to remove duplicate elements in an array. I tried to use `function alpha(arr)` to check each element against each other element next to each other (since I already alphabetized the array) but it is throwing an infinite loop. Is this the right way to go about this?

My code so far

``````  var zero = arr[0].toLowerCase();
var one = arr[1].toLowerCase();
var zeroarr = zero.split("");
var onearr = one.split("");
zeroarr.sort();
onearr.sort();

function alpha(arr) {
for(x = 0; x !== arr.size; x++) {
if(arr[x] == arr[x + 1]) {
arr.splice(x, 1);
}
}
}
alpha(zeroarr);
alpha(onearr);
var zerostring = zeroarr.toString();
var onestring = onearr.toString();
return zerostring.indexOf(onestring) !== -1;
}

mutation(["Mary", "Aarmy"]);
``````

Your browser information:

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36`.

Link to the challenge:

0 Likes

#2

Hey James,

Your for loop is infinite here because you meant to use .length instead of .size in the for loop.

0 Likes

#3

Pro tip: don’t modify an array that you’re iterating over. It is a classic way to get unexpected results and infinite loops.

0 Likes

#4

Thanks! this helped a lot!

0 Likes