Get duplicate characters in string

淺唱寂寞╮ 提交于 2019-11-29 17:46:26
Jonathan Lonowski

Well, I think falsetru had a good idea with a zero-width look-ahead.

'abcabc123123'.match(/(.+)(?=\1)/g)
// ["abc", "123"]

This allows it to match just the initial substring while ensuring at least 1 repetition follows.

For M42's follow-up example, it could be modified with a .*? to allow for gaps between repetitions.

'abc123ab12'.match(/(.+)(?=.*?\1)/g)
// ["ab", "12"]

Then, to find where the repetition starts with multiple uses together, a quantifier ({n}) can be added for the capture group:

'abcabc1234abc'.match(/(.+){2}(?=.*?\1)/g)
// ["abcabc"]

Or, to match just the initial with a number of repetitions following, add the quantifier within the look-ahead.

'abc123ab12ab'.match(/(.+)(?=(.*?\1){2})/g)
// ["ab"]

It can also match a minimum number of repetitions with a range quantifier without a max -- {2,}

'abcd1234ab12cd34bcd234'.match(/(.+)(?=(.*?\1){2,})/g)
// ["b", "cd", "2", "34"]

This solution may be used if you don't want to use regex:

function test() {
    var stringToTest = 'find the first duplicate character in the string';
    var a = stringToTest.split('');
    for (var i=0; i<a.length; i++) {
        var letterToCompare = a[i];
        for (var j=i+1; j<a.length; j++) {
            if (letterToCompare == a[j]) {
                console.log('first Duplicate found');
                console.log(letterToCompare);
                return false;
            }
        }
    }
}
test()

The answer above returns more duplicates than there actually are. The second for loop causes the problem and is unnecessary. Try this:

function stringParse(string){
  var arr = string.split("");
  for(var i = 0; i<arr.length; i++){
    var letterToCompare = arr[i];
    var j= i+1;
    if(letterToCompare === arr[j]){
      console.log('duplicate found');
      console.log(letterToCompare);
    }    
  }  
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!