简体   繁体   中英

How to remove all but one occurrences of 'abc' in a string?

Given the string 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc' , how do I remove all multiple occurrences but one of abc , acb ...

var str = 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc'

remove('abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc') 
//=> 'abc-acb-bac-bca-cab-cba'

I saw this question about removing duplicate characters from a string , but it only addresses repeated letters and I want to target specific substrings.

I would suggest using a split - filter - join combination, using a plain JavaScript object to store the set of substrings ( 'abc' , 'bac' , etc.) encountered so far, and returning false from filter when repeats are found so as to omit them from the result.

This has the added benefit of running in linear ( O(n) ) time relative to the length of the string, whereas most solutions using indexOf run at the much slower complexity of O(n 2 ) .

 function remove (string) { return string.split('-').filter(function (e) { return !this[e] && (this[e] = true) }, {}).join('-') } console.log(remove('abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc')) 

Edit : For anyone who is confused, the reference to this in the example above refers to the {} passed as the second argument to filter .

Use split('-') to remove all hyphens and filter() and indexOf() methods to return one of each element (all unique like an ES6 Set)

Just the mention of ES6 Set reminded me of an easier way to extract unique values from an array. See Snippet 2.

SNIPPET 1

 const str = `abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc`; // Use split on every hyphen const arr = str.split('-'); // Pass arr into function... function arrayUnique(arr) { // Filter arr with the results of... return arr.filter(function(ele, idx, arr) { /* ...the indexOf the current object || for each iteration, return only || if it matches current index */ return arr.indexOf(ele) === idx; }); } const res = arrayUnique(arr); console.log(res); 

SNIPPET 2

 const str = `abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc`; // split('-') to remove all hyphens const arr = str.split('-'); /* Use the Set Constructor || passing arr through an iterator || converting arr to a Set || a Set can only have unique || values */ const set = new Set(arr); // Do not use the SO Console, use the browser's console console.log(set); 

Try the following:

 var data = 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc'; var dataArr = data.split('-'); var resArr = []; dataArr.filter(function(val){ var i = resArr.findIndex(x => x == val); if(i <= -1) resArr.push(val); }); var resData = resArr.join('-'); console.log(resData) 

Split the string on the dashes, create a map keyed by each entry, get an array of all the keys from the map and join them.

function remove(str) {
  var map = {};
  str.split('-').forEach(
    piece => map[piece] = true
  );
  return Object.keys(map).join('-');
}

We can split the string into an array with the separator being - character and then find the unique elements in the array. Then simply join the array back into string with the delimiter being -

 // Function we'll use to get unique elements Array.prototype.getUnique = function() { var n = []; for (var i = 0; i < this.length; i++) { if (n.indexOf(this[i]) == -1) n.push(this[i]); } return n; } var str = 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc' var arr = str.split('-'); var arrUniq = arr.getUnique(); console.log(arrUniq.join('-')); 

pure javascript !

 var str = 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc'; str = str.split('-'); uniqueArray = str.filter(function(item, pos) { return str.indexOf(item) == pos; }) str = uniqueArray.join('-'); console.log(str); 

This Regex solves your problem:

/(\b\w+\b)-(?=.*\1)/g

Use like:

 function removeDuplicates(str){ return str.replace(/(\\b\\w+\\b)-(?=.*\\1)/g, ''); } removeDuplicates('abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM