Home > Web Front-end > JS Tutorial > body text

An example of string deduplication is shared

零下一度
Release: 2017-07-18 16:37:13
Original
1147 people have browsed it

How to achieve string deduplication? Usually split the string into an array and then operate on the array.

  
    var str = "aabdeegdcffdsf",
        result = [],
        tempStr = "";
    var removeDuplicate = function ( str ){
        var arr = str.split('');//把字符串分割成数组
        //arr.sort();//排序
        for(var i = 0; i < arr.length; i++){
            if(arr[i] !== tempStr){
                result.push(arr[i]);
                tempStr = arr[i];
            }else{
                continue;
            }
        }
        return result.join("");
    }
    console.log(removeDuplicate(str)); //abdegdcfdsf
Copy after login
I tried it, and the running results are as follows: abdegdcfdsf
was not completely deduplicated,
Modify it yourself:
var removeDuplicate = function ( str ){        var arr = str.split(&#39;&#39;);//把字符串分割成数组        //arr.sort();//排序        for(var i = 0; i <arr.length; i++){            if(tempStr.indexOf(arr[i])>=0){continue;                console.log(1)            }else{//console.log(2)                result.push(arr[i]);
                tempStr+=arr[i];console.log(tempStr);            }        }        return result.join("");    }
Copy after login

I tried it and the results are as follows: abdegcfds

The above is the detailed content of An example of string deduplication is shared. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template