admin 管理员组文章数量: 1086019
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
5 Answers
Reset to default 5You 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);
}
}
本文标签: javascriptHow to splice duplicate item in arrayStack Overflow
版权声明:本文标题:javascript - How to splice duplicate item in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744077954a2529699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论