JS---Remove or find repeated elements in the array

Topic 1: Remove a given element in the array.
Insert picture description here
Idea:
Because the topic requires operations on the given arr array, our department creates a new array to install other elements after the removed element. We can find other elements Method, here I use the splice method, we can see after clicking it: splice(Start:number,deleteCount?:number), that is, starting from Start:number, delete deleteCount?:number elements, we will see here There is an idea! First traverse the array, when judging the element equal to the given element, delete an element from i, so that the latter element will run to the front, we can delete the element equal to the given element, and then operate on the original array. After removing the elements that are equal to the given ones, you get an array of other elements!

Code display:

 function f1(item){
    
    
        let arr=[1,2,2,3,4,2,2]
        for (let i = 0; i < arr.length; i++) {
    
    
            if (item==arr[i]){
    
    
                arr.splice(i,1)     //splice(Start:number,deleteCount?:number)
                i--;        //因为每次删除一个元素后,后面的元素的下标就会-1,但是我们查找的时候,循环让i+1,所以我们只能会找到下一个元素的下标
                            //例如,当我们删除第二个元素后,后面的元素,即原始数组的第三个元素就会变为第二个,我们这时i变为了3,就不会再与改变后的数组第二个元素比较,导致这个元素不会被移除
            }
        }
        for (let a of arr) {
    
    
            console.log(a);     //遍历数组
        }
    }
    f1(2)

Topic 2: Finding repeated elements in the array
Insert picture description here
Idea: First, we need to find the duplicated element array, traverse the duplicated array again, and compare each element with the initial array. If they are equal, then The number of the element is +1. After the comparison is completed, we can get the number of times that all elements are equal to the array after deduplication. If the number of times is greater than 1, it means that the element exists more than once in the original array. Element output!

Code display:

<script>
	//去重
    let arr=[1,2,4,4,3,3,1,5,3];
    let removal=new Array(arr.length)
    for (let i = 0; i < arr.length; i++) {
    
    	
        let flag = false
        for (let n of removal) {
    
    
            if (n == arr[i]) {
    
    
                flag = true		//如果相等,变为true
                break
            }
        }
        if (!flag) {
    
    
            removal.push(arr[i])   //将去重的结果重新装进一个数组中
        }
    }

    for (let n of removal) {
    
           //遍历去重的数组
        count(n);         //调用函数,并将去重的数组中的元素作为参数传给函数
    }

    function count(x) {
    
       //创建一个函数
        let count=0;    //定义一个累加的次数
        for (let a of arr) {
    
        //遍历初始的数组
            if(a==x){
    
           //如果去重的数等于初始的数组中的元素,就将该数次数+1
                count++;
            }
        }
        if(count>1){
    
            //判断如果每个数的累加次数大于1时,说明该数在数组中是存在重复的
            console.log(x);     //输出
        }
    }
</script>

Guess you like

Origin blog.csdn.net/weixin_44889894/article/details/112635919