最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to splice duplicate item in array - Stack Overflow

programmeradmin0浏览0评论

I am making an attempt at writing a function in JavaScript that accepts an array that has duplicates in it and returns the same array removing duplicates. I know how to this using a Set, filter and reduce but I wanted to try to do it without using those functions. The problem is that I don't know how to splice the duplicate item once I have found them, so how can I just remove the item from the array if it is found as a duplicate, here is my code:

function clearDuplicatesInArray(arr, item) {
 for(i = 0; i < arr.length; i++) {
   for(j = 0; j < arr.length; j++) {
      if(arr[i] === arr[j]) {
       arr.splice(arr[i], arr[j])
      }
    }
  }
 return arr;
}

clearDuplicatesInArray([1,1,2,3]);

I am making an attempt at writing a function in JavaScript that accepts an array that has duplicates in it and returns the same array removing duplicates. I know how to this using a Set, filter and reduce but I wanted to try to do it without using those functions. The problem is that I don't know how to splice the duplicate item once I have found them, so how can I just remove the item from the array if it is found as a duplicate, here is my code:

function clearDuplicatesInArray(arr, item) {
 for(i = 0; i < arr.length; i++) {
   for(j = 0; j < arr.length; j++) {
      if(arr[i] === arr[j]) {
       arr.splice(arr[i], arr[j])
      }
    }
  }
 return arr;
}

clearDuplicatesInArray([1,1,2,3]);
Share Improve this question asked Oct 22, 2019 at 8:35 Mahma DevaMahma Deva 5663 gold badges10 silver badges27 bronze badges 4
  • 2 Possible duplicate of Get all unique values in a JavaScript array (remove duplicates) – Iurii Drozdov Commented Oct 22, 2019 at 8:38
  • 1 It is obviously not a duplicate. – Mahma Deva Commented Oct 22, 2019 at 8:40
  • Check out this link for more insight, hope it helps – Earl-V Commented Oct 22, 2019 at 8:42
  • @MahmaDeva that duplicate has 89 answers many of which are just using for loops without using any of the array methods. Also: Looping through array and removing items, without breaking for loop – adiga Commented Oct 22, 2019 at 8:49
Add a ment  | 

5 Answers 5

Reset to default 5

You could iterate form the end, to prevent slicing for indices whixh are not visited yet and use a second loop until the outer index, because over this index, you have already visited the items.

For splicing, take the index and one as parameter, which removes one element at the given index.

function clearDuplicatesInArray(array, item) {
    var i = array.length,
        j;
    
    while (--i) {
        for (j = 0; j < i; j++) {
            if (array[i] === array[j]) {
                array.splice(i, 1);
                break;
            }
        }
    }
    return array;
}

console.log(clearDuplicatesInArray([1, 1, 2, 3]));

I am just going to fix your code, although you can get the solution in many different ways.

function clearDuplicatesInArray(arr, item) {
 for(i = 0; i < arr.length; i++) {
   for(j = i+1; j < arr.length; j++) {
      if(arr[i] === arr[j]) {
       arr.splice(i, 1);
       i--;
       break;
      }
    }
  }
 return arr;
}

console.log(clearDuplicatesInArray([1,1,2,3]));

function removeDuplicates(array) {
   var tmp = array.slice(); //copy of array
   for(let i = 0, max = tmp.length; i < max; i++) {
      if(tmp.indexOf(tmp[i]) != tmp.lastIndexOf(tmp[i])) {
         tmp.splice(tmp.indexOf(tmp[i]), 1);
         i--;
      }
   }
   return tmp;
}

This function checks whether the first occurence of a value is also the last in the array, and if not, removes it.

Note that it uses slice and splice, 2 different functions.

input:
removeDuplicates([1,1,2,3,4,4,7]);

output:
Array(5) [ 1, 2, 3, 4, 7 ]

You can use the value as key to remove duplicate with O(n).

function clearDuplicatesInArray(array,item){
    var temp = {};
    for(v of array){
        temp[v] = v
    }
    return Object.values(temp)
}
console.log(clearDuplicatesInArray([1,1,1,2,1,4,2,3]))

Another way of doing it by writing a single loop is the following:

1) Sort the array.

2) Run an reverse loop (to avoid problems with splicing).

arr = arr.sort();

let i = arr.length;
while (--i) {
    if (arr[i] == arr[i-1]) {
    arr.splice(i, 1);
  }
}
发布评论

评论列表(0)

  1. 暂无评论