Remove occurrences of duplicate words in a string

前端 未结 9 1950
野性不改
野性不改 2020-11-27 07:37

Take the following string as an example:

var string = \"spanner, span, spaniel, span\";

From this string I would like to find the duplicate

相关标签:
9条回答
  • 2020-11-27 08:01
    var string = "spanner, span, spaniel, span";
    
    var strArray= string.split(",");
    
    var unique = [];
     for(var i =0; i< strArray.length; i++)
     {
       eval(unique[strArray] = new Object()); 
     }
    

    //You can easily traverse the unique through foreach.

    I like this for three reason. First, it works with IE8 or any other browser.

    Second. it is more optimized and guaranteed to have unique result.

    Last, It works for Other String array which has White space in their inputs like

    var string[] = {"New York", "New Jersey", "South Hampsire","New York"};
    

    for the above case there will be only three elements in the string[] which would be uniquely stored.

    0 讨论(0)
  • 2020-11-27 08:04

    To delete all duplicate words, I use this code:

    <script>
    function deleteDuplicate(a){a=a.toString().replace(/ /g,",");a=a.replace(/[ ]/g,"").split(",");for(var b=[],c=0;c<a.length;c++)-1==b.indexOf(a[c])&&b.push(a[c]);b=b.join(", ");return b=b.replace(/,/g," ")};
    document.write(deleteDuplicate("g g g g"));
    </script>
    
    0 讨论(0)
  • 2020-11-27 08:05

    Alternate Solution using Regular Expression

    By making use of positive lookahead, you can strip off all the duplicate words.

    Regex /(\b\S+\b)(?=.*\1)/ig, where

    • \b - matches word boundary
    • \S - matches character that is not white space(tabs, line breaks,etc)
    • ?= - used for positive lookahead
    • ig - flags for in-casesensitive,global search respectively
    • +,* - quantifiers. + -> 1 or more, * -> 0 or more
    • () - define a group
    • \1 - back-reference to the results of the previous group

    var string1 = 'spanner, span, spaniel, span';
    var string2 = 'spanner, span, spaniel, span, span';
    var string3 = 'What, the, the, heck';
    // modified regex to remove preceding ',' and ' ' as per your scenario 
    var result1 = string1.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
    var result2 = string2.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
    var result3 = string3.replace(/(\b, \w+\b)(?=.*\1)/ig, '');
    console.log(string1 + ' => ' + result1);
    console.log(string2 + ' => ' + result2);
    console.log(string3 + ' => ' + result3);

    The only caveat is that this regex keeps only the last instance of a found duplicate word and strips off all the rest. To those who care only about duplicates and not about the order of the words, this should work!

    0 讨论(0)
  • 2020-11-27 08:05

    below is an easy to understand and quick code to remove duplicate words in a string:

    var string = "spanner, span, spaniel, span";
    
    
    var uniqueListIndex=string.split(',').filter(function(currentItem,i,allItems){
        return (i == allItems.indexOf(currentItem));
    });
    
    var uniqueList=uniqueListIndex.join(',');
    
    alert(uniqueList);//Result:spanner, span, spaniel
    

    As simple as this can solve your problem. Hope this helps. Cheers :)

    0 讨论(0)
  • 2020-11-27 08:10
    <script type="text/javascript">
    str=prompt("Enter String::","");
    arr=new Array();
    arr=str.split(",");
    unique=new Array();
    for(i=0;i<arr.length;i++)
    {
        if((i==arr.indexOf(arr[i]))||(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])))
            unique.push(arr[i]);   
    }
    unique.join(",");
    alert(unique);
    </script>
    

    this code block will remove duplicate words from a sentence.

    the first condition of if statement i.e (i==arr.indexOf(arr[i])) will include the first occurence of a repeating word to the result(variale unique in this code).

    the second condition (arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])) will include all non repeating words.

    0 讨论(0)
  • 2020-11-27 08:12

    How about something like this?

    split the string, get the array, filter it to remove duplicate items, join them back.

    var uniqueList=string.split(',').filter(function(item,i,allItems){
        return i==allItems.indexOf(item);
    }).join(',');
    
    $('#output').append(uniqueList);
    

    Fiddle

    For non supporting browsers you can tackle it by adding this in your js.

    See Filter

    if (!Array.prototype.filter)
    {
      Array.prototype.filter = function(fun /*, thisp*/)
      {
        "use strict";
    
        if (this == null)
          throw new TypeError();
    
        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun != "function")
          throw new TypeError();
    
        var res = [];
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
        {
          if (i in t)
          {
            var val = t[i]; // in case fun mutates this
            if (fun.call(thisp, val, i, t))
              res.push(val);
          }
        }
    
        return res;
      };
    }
    
    0 讨论(0)
提交回复
热议问题