Answer:
function removeRepeaters(list){
var goodList = [], badList = {}, used = {}, n;
// ensure that the argument is indeed an array
if(!Array.isArray(list)){
throw "removeRepeaters: Expecting one argument of type Array";
}
// loop through the array and take note of any duplicates
for(n in list) used[list[n]] == true ? badList[list[n]] = true : used[list[n]] = true;
// now loop through again, and assemble a list of non-duplicates
for(n in list) if(badList[list[n]] == undefined) goodList[] = list[n];
return goodList;
}
Explanation:
I assume you're familiar with trinary operators, but just in case, that's what's happening in this first for loop:
for(n in list) used[list[n]] == true ? badList[list[n]] = true : used[list[n]] = true;
this is the same as saying:
for(n in list){
if(used[list[n]] == true){
badList[list[n]] = true;
} else {
used[list[n]] = true;
}
}
This loop flags all of the values in the list that are duplicated. Note that both "badList" and "used" are declared as objects instead of arrays. This allows us to compare keys in them with an == operator, even if they're not defined, making it a convenient way to flag things.
Note that I haven't tested it, so I may have overlooked something. I suggest testing it before handing it in.