-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
腾讯:不产生新数组,删除数组里的重复元素 #135
Labels
Comments
function setArr(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1);
j--
}
}
}
return arr
} |
function setArr(arr) {
for(let i = 0;i<arr.length;i++){
if(arr.indexOf(arr[i])!==i){
arr.splice(i,1);
i--
}
}
return arr
} |
let uniqueArr=function uniqueArr(arr){
let map={};
let len = arr.length-1;
while(len>=0){
let item = arr[len];
if(map[item]){
arr.splice(len,1)
}else{
map[item]=true;
}
len--;
}
return arr;
} |
function setArr(arr, map = new Map()) {
let idx = arr.length - 1;
while (idx >= 0) {
if (map.get(arr[idx])) {
arr.splice(idx, 1)
} else {
map.set(arr[idx], true)
}
idx--;
}
return arr
} |
|
const setArr = arr => {// 利用对象属性唯一性
} |
// 双指针 let slow = -1; return nums.slice(0, slow + 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
数组去重的方式有很多,我们可以使用 Set 去重、filter 过滤等,详见 携程&蘑菇街&bilibili:手写数组去重、扁平化函数 ,但三种解法(Set、filter、reducer)都产生了新数组:
那么我们如何在不产生性数组的情况下删除数组中的重复元素喃?
方式一:排序去重
方式二:优化
The text was updated successfully, but these errors were encountered: