Regex - find duplicate sets, keep them and remove everything else

What is the regex needed to remove all except second+ occurrence of the duplicate entries? data sets are separated by commas.

Example: This needs to convert to

#20131229PV1,#20140109PV5,#20140101PV1,#20140109PV5,#20140109PV5,#20131224PV5,

This

#20140109PV5,#20140109PV5,

after going through regex

Upvotes: 0

Views: 54

Answers (2)

Answered through a different approach (Javascript)

function find_duplicates(arr) {
var len=arr.length,
  out=[],
  counts={};

for (var i=0;i<len;i++) {
var item = arr[i];
var count = counts[item];
counts[item] = counts[item] >= 1 ? counts[item] + 1 : 1;
}

for (var item in counts) {
if(counts[item] > 1)
  out.push(item);
}

alert(out);
}

find_duplicates(bookcaldatesci.innerHTML.split(","));

Upvotes: 0

Chirag Bhatia - chirag64
Chirag Bhatia - chirag64

Reputation: 4526

Unfortunately, there is no way to find duplicate string sets using regex. You need a good string-based algorithm and implement it in your favorite computer language to achieve this.

Upvotes: 1

Related Questions