Removing duplicate strings using javascript

后端 未结 6 1841
耶瑟儿~
耶瑟儿~ 2021-01-15 00:27

I have an array of 800 sentences. I want to remove all duplicates (sentences that have the same exact words, but in different order) from the array. So for example \"this is

6条回答
  •  北恋
    北恋 (楼主)
    2021-01-15 01:16

    Sort the array of sentences, and then loop through it and delete an item if it is the same as the previous one:

    texts.sort();
    for(var i = 1; i < texts.length; i++){
        if(texts[i] === texts[i-1]){
            texts.splice(i,1);
            i--;
         }
    }
    

    I tested this in an array with 800 strings, and it seemed reasonably fast.

    EDIT: Sorry, didn't read your question very carefully

提交回复
热议问题