views:

403

answers:

3

What would be a nice algorithm to remove dupes on an array like below...

    var allwords = [

    ['3-hidroxitiramina', '3-hidroxitiramina'],

    ['3-hidroxitiramina', '3-hidroxitiramina'],

    ['3-in-1 block', 'bloqueo 3 en 1'],

    ['abacterial', 'abacteriano'],

    ['abacteriano', 'abacteriano'],

    ['abciximab', 'abciximab'],

...

Just to clarify, I would want one of the

['3-hidroxitiramina', '3-hidroxitiramina'],

To be removed, so there is just one

A: 

You could use an object as an associative array/hash (If you mean dups in the first dimension)

var allWordsObj = {};
for( var i = 0; i < allWords.length; i++ ) {
    allWordsObj[allWords[i][0]] = allWords[i][1];
}

alert( allWordsObj['3-hidroxitiramina'] );
meouw
I dont, but thanks
qui
A: 

Try this:

var len = allwords.length, i, j, first;
for (i=0; i<len-1; i++) {
    first = allwords[i].toString();
    for (j=i+1; j<len; j++) {
        if (first === allwords[j].toString()) {
            allwords.splice(j, 1);
            len--;
        }
    }
}
Gumbo
+1  A: 

[edit]: misread, after reading your clarification I'd suggest:

 var i = allwords.length-1, prev='';
 do {
     if (allwords[i].join('/') === prev) {
     allwords.splice(i,1);
     }
     prev = allwords[i].join('/');
 } while (i-- && i>-1);

(reversing the loop is a optimization step)

KooiInc
This seems to do the trick, thanks :)
qui