在整数数组B中寻找所有在整数数组A中不存在的元素(只能使用原生js不使用数组对象的方法)

// 在整数数组B中寻找所有在整数数组A中不存在的元素(只能使用原生js不使用数组对象的方法)
let A = [2, 18, 75, 11, 135, 472]
let B = [3, 75, 66, 93, 135]
// 最终结果
let resArr = []
// 排序
function mySort(obj) {
    for (let i = 0; i < obj.length - 1; i++) {
        for (let n = i + 1; n < obj.length; n++) {
            if (obj[i] > obj[n]) {
                let temp = obj[i]
                obj[i] = obj[n]
                obj[n] = temp
            }
        }
    }
}
// 现将A,B两个数组排序
mySort(A)
mySort(B)
// 进行运算
for (i = 0, j = 0; i < A.length, j < B.length;) {
    if (A[i] > B[j]) {
        resArr.push(B[j]);
        j++;
    } else if (A[i] == B[j]) {
        i++;
        j++;
    } else {
        i++;
    }
    if (i == A.length) {
        //B[j]之后的元素;
        for (j = i; j < B.length; j++) {
            resArr.push(B[j]);
        }
    }
}
console.log(resArr) // 3,66,93
posted @ 2022-01-24 14:43  ALin_Da  阅读(186)  评论(0编辑  收藏  举报