Remove occurrences of duplicate words in a string

前端 未结 9 2003
野性不改
野性不改 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 08:10

    
    

    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.

提交回复
热议问题