Remove Only One Duplicate from An Array
I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:
var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));
and:
var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));
Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:
Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}
But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:
Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}
Does anyone know how to only remove one of the 2s? Thanks for any help here.
javascript
add a comment |
I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:
var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));
and:
var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));
Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:
Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}
But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:
Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}
Does anyone know how to only remove one of the 2s? Thanks for any help here.
javascript
1
create a simple loop, when found an index with value of 2:.splice(idx, 1)
this index andbreak
the loop
– Calvin Nunes
Nov 19 '18 at 10:34
1
Which one do you want to remove ? The first2
, or last, or ...
– Mihai Alexandru-Ionut
Nov 19 '18 at 10:34
1
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Haroldo_OK
Nov 19 '18 at 10:38
add a comment |
I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:
var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));
and:
var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));
Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:
Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}
But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:
Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}
Does anyone know how to only remove one of the 2s? Thanks for any help here.
javascript
I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:
var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));
and:
var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));
Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:
Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}
But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:
Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
return Array.from(new Set(a))
}
Does anyone know how to only remove one of the 2s? Thanks for any help here.
javascript
javascript
asked Nov 19 '18 at 10:32
user8758206user8758206
456110
456110
1
create a simple loop, when found an index with value of 2:.splice(idx, 1)
this index andbreak
the loop
– Calvin Nunes
Nov 19 '18 at 10:34
1
Which one do you want to remove ? The first2
, or last, or ...
– Mihai Alexandru-Ionut
Nov 19 '18 at 10:34
1
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Haroldo_OK
Nov 19 '18 at 10:38
add a comment |
1
create a simple loop, when found an index with value of 2:.splice(idx, 1)
this index andbreak
the loop
– Calvin Nunes
Nov 19 '18 at 10:34
1
Which one do you want to remove ? The first2
, or last, or ...
– Mihai Alexandru-Ionut
Nov 19 '18 at 10:34
1
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Haroldo_OK
Nov 19 '18 at 10:38
1
1
create a simple loop, when found an index with value of 2:
.splice(idx, 1)
this index and break
the loop– Calvin Nunes
Nov 19 '18 at 10:34
create a simple loop, when found an index with value of 2:
.splice(idx, 1)
this index and break
the loop– Calvin Nunes
Nov 19 '18 at 10:34
1
1
Which one do you want to remove ? The first
2
, or last, or ...– Mihai Alexandru-Ionut
Nov 19 '18 at 10:34
Which one do you want to remove ? The first
2
, or last, or ...– Mihai Alexandru-Ionut
Nov 19 '18 at 10:34
1
1
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Haroldo_OK
Nov 19 '18 at 10:38
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Haroldo_OK
Nov 19 '18 at 10:38
add a comment |
6 Answers
6
active
oldest
votes
You could use indexOf
method in combination with splice
.
var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
arr.splice(idx, 1);
}
console.log(arr);
Close, but it won't work correctly if the number isn't present on the array; checking ifindexOf
is non-negative would make it more robust.
– Haroldo_OK
Nov 19 '18 at 10:47
1
brilliant, thank you
– user8758206
Nov 19 '18 at 12:05
add a comment |
You could take a closure with a counter and remove only the first 2
.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(1));
console.log(result);
For any other 2
, you could adjust the start value for decrementing.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(2));
console.log(result);
interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?
– user8758206
Nov 19 '18 at 16:44
1
one is to remove the first occurence of2
and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).
– Nina Scholz
Nov 19 '18 at 17:00
you're obviously a very advanced coder - thank you
– user8758206
Nov 19 '18 at 20:41
add a comment |
you can follow the following method
var arr= [2,3,4,2,4,5];
var unique = ;
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
})
add a comment |
There are various ways to do that; one relatively simple way would be to use indexOf
; see this other post: https://stackoverflow.com/a/5767357/679240
var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);
add a comment |
You can do:
const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
.reduce((a, c) => {
a.temp[c] = ++a.temp[c] || 1;
if (a.temp[c] !== 2) {
a.array.push(c);
}
return a;
}, {temp: {}, array: })
.array;
console.log(result);
add a comment |
Most simple way to filter all duplicates from array:
arr.filter((item, position) => arr.indexOf(item) === position)
This method skip element if another element with the same value already exist.
If you need to filter only first duplicate, you can use additional bool key:
arr.filter((item, position) => {
if (!already && arr.indexOf(item) !== position) {
already = true
return false
} else return true
})
But this method have overheaded. Smartest way is use for
loop:
for (let i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== i) {
arr.splice(i,1);
break;
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372690%2fremove-only-one-duplicate-from-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use indexOf
method in combination with splice
.
var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
arr.splice(idx, 1);
}
console.log(arr);
Close, but it won't work correctly if the number isn't present on the array; checking ifindexOf
is non-negative would make it more robust.
– Haroldo_OK
Nov 19 '18 at 10:47
1
brilliant, thank you
– user8758206
Nov 19 '18 at 12:05
add a comment |
You could use indexOf
method in combination with splice
.
var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
arr.splice(idx, 1);
}
console.log(arr);
Close, but it won't work correctly if the number isn't present on the array; checking ifindexOf
is non-negative would make it more robust.
– Haroldo_OK
Nov 19 '18 at 10:47
1
brilliant, thank you
– user8758206
Nov 19 '18 at 12:05
add a comment |
You could use indexOf
method in combination with splice
.
var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
arr.splice(idx, 1);
}
console.log(arr);
You could use indexOf
method in combination with splice
.
var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
arr.splice(idx, 1);
}
console.log(arr);
var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
arr.splice(idx, 1);
}
console.log(arr);
var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
arr.splice(idx, 1);
}
console.log(arr);
edited Nov 19 '18 at 10:49
Haroldo_OK
3,32521847
3,32521847
answered Nov 19 '18 at 10:39
Mihai Alexandru-IonutMihai Alexandru-Ionut
30.3k63971
30.3k63971
Close, but it won't work correctly if the number isn't present on the array; checking ifindexOf
is non-negative would make it more robust.
– Haroldo_OK
Nov 19 '18 at 10:47
1
brilliant, thank you
– user8758206
Nov 19 '18 at 12:05
add a comment |
Close, but it won't work correctly if the number isn't present on the array; checking ifindexOf
is non-negative would make it more robust.
– Haroldo_OK
Nov 19 '18 at 10:47
1
brilliant, thank you
– user8758206
Nov 19 '18 at 12:05
Close, but it won't work correctly if the number isn't present on the array; checking if
indexOf
is non-negative would make it more robust.– Haroldo_OK
Nov 19 '18 at 10:47
Close, but it won't work correctly if the number isn't present on the array; checking if
indexOf
is non-negative would make it more robust.– Haroldo_OK
Nov 19 '18 at 10:47
1
1
brilliant, thank you
– user8758206
Nov 19 '18 at 12:05
brilliant, thank you
– user8758206
Nov 19 '18 at 12:05
add a comment |
You could take a closure with a counter and remove only the first 2
.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(1));
console.log(result);
For any other 2
, you could adjust the start value for decrementing.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(2));
console.log(result);
interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?
– user8758206
Nov 19 '18 at 16:44
1
one is to remove the first occurence of2
and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).
– Nina Scholz
Nov 19 '18 at 17:00
you're obviously a very advanced coder - thank you
– user8758206
Nov 19 '18 at 20:41
add a comment |
You could take a closure with a counter and remove only the first 2
.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(1));
console.log(result);
For any other 2
, you could adjust the start value for decrementing.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(2));
console.log(result);
interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?
– user8758206
Nov 19 '18 at 16:44
1
one is to remove the first occurence of2
and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).
– Nina Scholz
Nov 19 '18 at 17:00
you're obviously a very advanced coder - thank you
– user8758206
Nov 19 '18 at 20:41
add a comment |
You could take a closure with a counter and remove only the first 2
.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(1));
console.log(result);
For any other 2
, you could adjust the start value for decrementing.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(2));
console.log(result);
You could take a closure with a counter and remove only the first 2
.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(1));
console.log(result);
For any other 2
, you could adjust the start value for decrementing.
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(2));
console.log(result);
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(1));
console.log(result);
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(1));
console.log(result);
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(2));
console.log(result);
var array = [2, 7, 9, 2, 3, 2, 5, 2],
result = array.filter((i => v => v !== 2 || --i)(2));
console.log(result);
edited Nov 19 '18 at 10:48
answered Nov 19 '18 at 10:42
Nina ScholzNina Scholz
182k1495164
182k1495164
interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?
– user8758206
Nov 19 '18 at 16:44
1
one is to remove the first occurence of2
and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).
– Nina Scholz
Nov 19 '18 at 17:00
you're obviously a very advanced coder - thank you
– user8758206
Nov 19 '18 at 20:41
add a comment |
interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?
– user8758206
Nov 19 '18 at 16:44
1
one is to remove the first occurence of2
and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).
– Nina Scholz
Nov 19 '18 at 17:00
you're obviously a very advanced coder - thank you
– user8758206
Nov 19 '18 at 20:41
interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?
– user8758206
Nov 19 '18 at 16:44
interesting answer. What does the v and (1) and (2) represent? I'm new to JS and haven't seen this type of syntax before - can you link to anything I can read to learn this?
– user8758206
Nov 19 '18 at 16:44
1
1
one is to remove the first occurence of
2
and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).– Nina Scholz
Nov 19 '18 at 17:00
one is to remove the first occurence of
2
and two for the second, and so on. it works, because the number gets decremented and if zero, the value is filterd out. the technique's name is closure with an IIFE (immediately-invoked function expression).– Nina Scholz
Nov 19 '18 at 17:00
you're obviously a very advanced coder - thank you
– user8758206
Nov 19 '18 at 20:41
you're obviously a very advanced coder - thank you
– user8758206
Nov 19 '18 at 20:41
add a comment |
you can follow the following method
var arr= [2,3,4,2,4,5];
var unique = ;
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
})
add a comment |
you can follow the following method
var arr= [2,3,4,2,4,5];
var unique = ;
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
})
add a comment |
you can follow the following method
var arr= [2,3,4,2,4,5];
var unique = ;
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
})
you can follow the following method
var arr= [2,3,4,2,4,5];
var unique = ;
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
})
answered Nov 19 '18 at 10:42
Tijo JohnTijo John
372516
372516
add a comment |
add a comment |
There are various ways to do that; one relatively simple way would be to use indexOf
; see this other post: https://stackoverflow.com/a/5767357/679240
var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);
add a comment |
There are various ways to do that; one relatively simple way would be to use indexOf
; see this other post: https://stackoverflow.com/a/5767357/679240
var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);
add a comment |
There are various ways to do that; one relatively simple way would be to use indexOf
; see this other post: https://stackoverflow.com/a/5767357/679240
var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);
There are various ways to do that; one relatively simple way would be to use indexOf
; see this other post: https://stackoverflow.com/a/5767357/679240
var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);
var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);
var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);
edited Nov 19 '18 at 10:43
answered Nov 19 '18 at 10:38
Haroldo_OKHaroldo_OK
3,32521847
3,32521847
add a comment |
add a comment |
You can do:
const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
.reduce((a, c) => {
a.temp[c] = ++a.temp[c] || 1;
if (a.temp[c] !== 2) {
a.array.push(c);
}
return a;
}, {temp: {}, array: })
.array;
console.log(result);
add a comment |
You can do:
const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
.reduce((a, c) => {
a.temp[c] = ++a.temp[c] || 1;
if (a.temp[c] !== 2) {
a.array.push(c);
}
return a;
}, {temp: {}, array: })
.array;
console.log(result);
add a comment |
You can do:
const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
.reduce((a, c) => {
a.temp[c] = ++a.temp[c] || 1;
if (a.temp[c] !== 2) {
a.array.push(c);
}
return a;
}, {temp: {}, array: })
.array;
console.log(result);
You can do:
const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
.reduce((a, c) => {
a.temp[c] = ++a.temp[c] || 1;
if (a.temp[c] !== 2) {
a.array.push(c);
}
return a;
}, {temp: {}, array: })
.array;
console.log(result);
const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
.reduce((a, c) => {
a.temp[c] = ++a.temp[c] || 1;
if (a.temp[c] !== 2) {
a.array.push(c);
}
return a;
}, {temp: {}, array: })
.array;
console.log(result);
const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
.reduce((a, c) => {
a.temp[c] = ++a.temp[c] || 1;
if (a.temp[c] !== 2) {
a.array.push(c);
}
return a;
}, {temp: {}, array: })
.array;
console.log(result);
edited Nov 19 '18 at 10:46
answered Nov 19 '18 at 10:38
Yosvel QuinteroYosvel Quintero
11.1k42429
11.1k42429
add a comment |
add a comment |
Most simple way to filter all duplicates from array:
arr.filter((item, position) => arr.indexOf(item) === position)
This method skip element if another element with the same value already exist.
If you need to filter only first duplicate, you can use additional bool key:
arr.filter((item, position) => {
if (!already && arr.indexOf(item) !== position) {
already = true
return false
} else return true
})
But this method have overheaded. Smartest way is use for
loop:
for (let i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== i) {
arr.splice(i,1);
break;
}
}
add a comment |
Most simple way to filter all duplicates from array:
arr.filter((item, position) => arr.indexOf(item) === position)
This method skip element if another element with the same value already exist.
If you need to filter only first duplicate, you can use additional bool key:
arr.filter((item, position) => {
if (!already && arr.indexOf(item) !== position) {
already = true
return false
} else return true
})
But this method have overheaded. Smartest way is use for
loop:
for (let i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== i) {
arr.splice(i,1);
break;
}
}
add a comment |
Most simple way to filter all duplicates from array:
arr.filter((item, position) => arr.indexOf(item) === position)
This method skip element if another element with the same value already exist.
If you need to filter only first duplicate, you can use additional bool key:
arr.filter((item, position) => {
if (!already && arr.indexOf(item) !== position) {
already = true
return false
} else return true
})
But this method have overheaded. Smartest way is use for
loop:
for (let i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== i) {
arr.splice(i,1);
break;
}
}
Most simple way to filter all duplicates from array:
arr.filter((item, position) => arr.indexOf(item) === position)
This method skip element if another element with the same value already exist.
If you need to filter only first duplicate, you can use additional bool key:
arr.filter((item, position) => {
if (!already && arr.indexOf(item) !== position) {
already = true
return false
} else return true
})
But this method have overheaded. Smartest way is use for
loop:
for (let i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== i) {
arr.splice(i,1);
break;
}
}
answered Nov 19 '18 at 11:21
XeelleyXeelley
314
314
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372690%2fremove-only-one-duplicate-from-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
create a simple loop, when found an index with value of 2:
.splice(idx, 1)
this index andbreak
the loop– Calvin Nunes
Nov 19 '18 at 10:34
1
Which one do you want to remove ? The first
2
, or last, or ...– Mihai Alexandru-Ionut
Nov 19 '18 at 10:34
1
Possible duplicate of How do I remove a particular element from an array in JavaScript?
– Haroldo_OK
Nov 19 '18 at 10:38