代码如下:
let arr = [1, 6, 8, 3, 7, 9, 2, 7, 2, 4, 4, 3, 3, 1, 5, 3] Array.prototype.removeDuplication = function () { let duplication = new Set() // let this = this this.map((item, index) => { let lastIndexOfItem = this.lastIndexOf(item) while (index !== lastIndexOfItem) { duplication.add(item) this.splice(lastIndexOfItem, 1) lastIndexOfItem = this.lastIndexOf(item) } }) return duplication } console.log(arr.removeDuplication()) console.log(arr)