我有一个这样的数组
var randomArray = [1,2,1,1,1,1,0,2,1,2,3,10,12,54,10,12] etc..
我可以删除重复元素或在其中找到重复元素。但我想记录数组中重复的所有重复元素序列。这是我尝试过的代码,但它正在进入无限循环
for (i = 0; i < randomLength; i++) {
var cycle = [i],
flag = 0,
start = i;
for (var j = i + 1; j < randomLength; j++) {
if (randomArray[i] == randomArray[j]) {
cycle.push(randomArray[j]);
while (i <= j) {
if (randomArray[i + 1] == randomArray[j + 1]) {
cycle.push(randomArray[j + 1]);
}
i = i + 1;
j = j + 1;
}
console.log(cycle);
}
i = start;
}
i = start;
}
它应该还给我。而且我不想用正则表达式做同样的事情
1,2
1,1
10,12
If array is ["a","d","z","e","g","h","a","d","z"]
然后
output would be "a","d","z"
它应该是最佳解决方案。请就此向我提出建议。至少更正我当前的代码..