Native JS --- array deduplication summary

Reprinted from: Click here to view the source of reprint

1, two-cycle deduplication

Double for (or while) loop is relatively clumsy way, it implements the principle is simple: to define an array of first element of the array contains the original, then traverse the original array, each element of the original array with the new array each element of the comparison, if it is not repeated to the new array, and returns a new array; because its time complexity is O (n ^ 2), if the length of the array is large, it will be very memory-intensive.

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    let res = [arr[0]]
    for (let i = 1; i < arr.length; i++) {
        let flag = true
        for (let j = 0; j < res.length; j++) {
            if (arr[i] === res[j]) {
                flag = false;
                break
            }
        }
        if (flag) {
            res.push(arr[i])
        }
    }
    return res
}

 


 

2, indexOf () deduplication

IndexOf array () method returns the position of the first occurrence of a specified element in the array. The first method defines an empty array res, and then call the original method indexOf traverse the array is determined, if the element is not in res, which will push into the res, and finally returns to the res array to get heavy. ( Judge whether the new whether creating an array in a [i], if no indexOf returns -1, and the a [i] into the new array )

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    let res = []
    for (let i = 0; i < arr.length; i++) {
        if (res.indexOf(arr[i]) === -1) {
            res.push(arr[i])
        }
    }
    return res
}

 


 

 

3, adjacent to the heavy elements

This first method was called first sort sorted array () , and then traverse the sorted according to the result of adjacent elements match , if equal skip change element, until the end of traversal.

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    arr = arr.sort()
    let res = [arr[0]]
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] !== arr[i-1]) {
            res.push(arr[i])
        }
    }
    return res
}

 


 

 

4, object properties deduplication (recommended, since only need to traverse it again)

Create an empty object , through the array, the array is set to the value of the object's properties, and the properties assigned to the initial value of 1, each appeared once, the corresponding property value increased by 1, so is the number of times the element can occur corresponding property value a. a subject, the absence arr [i] property, the array push, obj added arr [i] properties

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    let res = [],
        obj = {}
    for (let i = 0; i < arr.length; i++) {
        if (!obj[arr[i]]) {
            res.push(arr[i])
            obj[arr[i]] = 1
        } else {
            obj[arr[i]]++
        }
    }
    return res
}

 


 

 

5, set to re-assignment and deconstruction (recommended, convenient, fast)

ES6 the new data type set, one characteristic set of data is not repeated . Set function can accept an array (or array-like object) as a parameter initialization can be done using this property to the array to weight.

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    return [...new Set(arr)]
}

 


 

 

6, set the Array.from (recommended, convenient, fast)

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    return Array.from(new Set(arr))
}

 

Reprinted from: Click here to view the source of reprint

1, two-cycle deduplication

Double for (or while) loop is relatively clumsy way, it implements the principle is simple: to define an array of first element of the array contains the original, then traverse the original array, each element of the original array with the new array each element of the comparison, if it is not repeated to the new array, and returns a new array; because its time complexity is O (n ^ 2), if the length of the array is large, it will be very memory-intensive.

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    let res = [arr[0]]
    for (let i = 1; i < arr.length; i++) {
        let flag = true
        for (let j = 0; j < res.length; j++) {
            if (arr[i] === res[j]) {
                flag = false;
                break
            }
        }
        if (flag) {
            res.push(arr[i])
        }
    }
    return res
}

 


 

2, indexOf () deduplication

IndexOf array () method returns the position of the first occurrence of a specified element in the array. The first method defines an empty array res, and then call the original method indexOf traverse the array is determined, if the element is not in res, which will push into the res, and finally returns to the res array to get heavy. ( Judge whether the new whether creating an array in a [i], if no indexOf returns -1, and the a [i] into the new array )

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    let res = []
    for (let i = 0; i < arr.length; i++) {
        if (res.indexOf(arr[i]) === -1) {
            res.push(arr[i])
        }
    }
    return res
}

 


 

 

3, adjacent to the heavy elements

This first method was called first sort sorted array () , and then traverse the sorted according to the result of adjacent elements match , if equal skip change element, until the end of traversal.

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    arr = arr.sort()
    let res = [arr[0]]
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] !== arr[i-1]) {
            res.push(arr[i])
        }
    }
    return res
}

 


 

 

4, object properties deduplication (recommended, since only need to traverse it again)

Create an empty object , through the array, the array is set to the value of the object's properties, and the properties assigned to the initial value of 1, each appeared once, the corresponding property value increased by 1, so is the number of times the element can occur corresponding property value a. a subject, the absence arr [i] property, the array push, obj added arr [i] properties

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    let res = [],
        obj = {}
    for (let i = 0; i < arr.length; i++) {
        if (!obj[arr[i]]) {
            res.push(arr[i])
            obj[arr[i]] = 1
        } else {
            obj[arr[i]]++
        }
    }
    return res
}

 


 

 

5, set to re-assignment and deconstruction (recommended, convenient, fast)

ES6 the new data type set, one characteristic set of data is not repeated . Set function can accept an array (or array-like object) as a parameter initialization can be done using this property to the array to weight.

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    return [...new Set(arr)]
}

 


 

 

6, set the Array.from (recommended, convenient, fast)

function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    return Array.from(new Set(arr))
}

 

Guess you like

Origin www.cnblogs.com/zero18/p/11001895.html