Removing duplicate strings using javascript

后端 未结 6 1839
耶瑟儿~
耶瑟儿~ 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 00:57

    This is a very simple implementation that takes advantage of some jQuery.

    Check the demo here ->

    And the source:

    var arr = ["This is a sentence", "Is this a sentence", "potatoes"];
    var newArr = [];
    var sortedArr = [];
    $.each(arr, function(i) {
        var temp = this.toLowerCase().split(" ").sort(function(a,b) {
                return a > b;
        }).join(' ');
        if ($.inArray(temp, sortedArr) == -1) {
            sortedArr.push(temp);
            newArr.push(arr[i]);   
        }
    });
    
    //output
    $.each(newArr, function() {
        document.write(this + '
    '); });

    It uses three arrays: a source, a collection of sorted sentences to match against, and the output array. Matching is performed by splitting a sentence by spaces, converting to lowercase, and sorting the words alphabetically, then rebuilding the sentence string. If that particular combo has been seen before, it is not added to the results. If it has not, it is added.

    The loop at the end simply outputs the resultant array.

提交回复
热议问题