LearningRecord icon indicating copy to clipboard operation
LearningRecord copied to clipboard

在一个无序数组中,寻找连续两个及两个以上的相同元素的个数

Open Rashomon511 opened this issue 5 years ago • 1 comments

思路:利用对象属性值的唯一性

        var arr = [0,1, 1,0,3,12, 12, 3, 1,2, 3, 3, 3]
        const getNum = (arr) => {
            let obj = {}
            for(let i = 0; i < arr.length - 1; i++){
                if (arr[i] === arr[i+1]){
                    obj[arr[i]] = arr[i];
                }
            }
            return Object.keys(obj).length

        }
        getNum(arr);

Rashomon511 avatar May 27 '19 09:05 Rashomon511

你这代码有问题吧?输出3,应该是4

huidt avatar Sep 23 '20 12:09 huidt