1 回答
TA贡献1802条经验 获得超5个赞
var arr = [1,2,3,9,9,9,9,6,7,8,10,10,10,15]
var dic = {}
for (k in arr){
if (!dic[arr[k]]){dic[arr[k]] = [k]}
else{dic[arr[k]][1] = k}
}
for (k in dic){if (dic[k].length==1){delete(dic[k])}}
console.log(dic)
{ '9': [ '3', '6' ], '10': [ '10', '12' ] }
var arr = [1,2,3,9,9,9,9,6,7,9,9,9,8,10,10,10,15,10,10]
var dic = {}
for (k in arr){
k = Number(k)
if(arr[k]==arr[k-1]||arr[k]==arr[k+1]){
if (!dic[arr[k]]){dic[arr[k]] = [[k,k]];continue}
s = dic[arr[k]].slice(-1)[0]
if(k-s[1]==1){s[1]=k}
else{dic[arr[k]].push([k,k])}
}
}
console.log(dic)
{ '9': [ [ 3, 6 ], [ 9, 11 ] ],
'10': [ [ 13, 15 ], [ 17, 18 ] ] }
添加回答
举报