Removing duplicate strings using javascript

后端 未结 6 1838
耶瑟儿~
耶瑟儿~ 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:18

    How about this?

    va = ["this is a sentence", "sentence this is", "sentence this is a"]
    vb = {} // dictionary of combined sorted words in each sentence
    vc = [] // output list of sentences without duplicates 
    
    for (i=0;i a is sentence this)
        var combined = va[i].split(" ").sort().join(" "); 
    
        if (!vb[combined]){       // if set of combined sorted words doesn't exist already
            vc.push(va[i]);      // sentence isn't duplicated, push to output list
            vb[combined] = true  // add set to dictionary
        }
    }
    
    alert(vc.join("\n"))
    

提交回复
热议问题