Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Created May 26, 2014 15:07
Show Gist options
  • Save jikeytang/2dff9493143e25ab8463 to your computer and use it in GitHub Desktop.
Save jikeytang/2dff9493143e25ab8463 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140527-题目1
一个字符串,找出这个字符串的第一对重复的字符
如 abcdefgjikuoa 第一对重复的字符是’a’
如 abcdegfeter 第一对重复的字符 ‘e’
PS:
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
// you code
```
2. 粘贴代码时请使用shift+tab,缩进前面的空白。
@rambo-panda
Copy link

@mailzwj 这个就不行啊
'dsgfdhsfdsgasfsdhyfguj'

@itbeihe
Copy link

itbeihe commented May 27, 2014

写完之后发现跟@ wzc602003869 基本一样,基本方法都被写全了。

function findRepeat(str){
    var arr = str.split(''),
        len = arr.length;
    for(var i=1;i<=len;i++){
        for(var j=i;j<i;j++){
            if(arr[i]==arr[j]){
                return arr[i];
            }
        }
    }
return false;
}
console.log(findRepeat('abcdefgg'));

把所有方法转移到放到http://jsperf.com/find-repeat 上跑了下。@mailzwj的方法遇到无重复或多个连续重复有问题,所有没跑。

@styling
Copy link

styling commented May 30, 2014

真是凶啊 没啥写的了

function a (x) {
    var item;
    for(var i = 0; i < x.length; i++){
        if(x.split(x[i]).length > 2) return x[i];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment