Leetcode: Remove duplicates from sorted array (Javascript)
Why does my solution work in the console but not on leetcode?
var removeDuplicates = function(nums) {
let res = ;
for(let num of nums) {
if(res.includes(num) === false) {
res.push(num);
}
}
return res.length;
};
Console:
screenshot
Leetcode:
let arr = [1, 1, 2]
removeDuplicates(arr) // 3
javascript algorithm
add a comment |
Why does my solution work in the console but not on leetcode?
var removeDuplicates = function(nums) {
let res = ;
for(let num of nums) {
if(res.includes(num) === false) {
res.push(num);
}
}
return res.length;
};
Console:
screenshot
Leetcode:
let arr = [1, 1, 2]
removeDuplicates(arr) // 3
javascript algorithm
have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)
– Venkatesh Konatham
Nov 22 '18 at 7:01
add a comment |
Why does my solution work in the console but not on leetcode?
var removeDuplicates = function(nums) {
let res = ;
for(let num of nums) {
if(res.includes(num) === false) {
res.push(num);
}
}
return res.length;
};
Console:
screenshot
Leetcode:
let arr = [1, 1, 2]
removeDuplicates(arr) // 3
javascript algorithm
Why does my solution work in the console but not on leetcode?
var removeDuplicates = function(nums) {
let res = ;
for(let num of nums) {
if(res.includes(num) === false) {
res.push(num);
}
}
return res.length;
};
Console:
screenshot
Leetcode:
let arr = [1, 1, 2]
removeDuplicates(arr) // 3
javascript algorithm
javascript algorithm
edited Nov 22 '18 at 7:03
annieg4123
asked Nov 22 '18 at 6:58
annieg4123annieg4123
915
915
have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)
– Venkatesh Konatham
Nov 22 '18 at 7:01
add a comment |
have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)
– Venkatesh Konatham
Nov 22 '18 at 7:01
have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)
– Venkatesh Konatham
Nov 22 '18 at 7:01
have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)
– Venkatesh Konatham
Nov 22 '18 at 7:01
add a comment |
2 Answers
2
active
oldest
votes
You can try changing includes
to indexOf
, may be includes
is not working in your environment. Also, instead of returning length
you should return res
.
Just in case you want to try another approach, you can look at Sets
like below
var removeDuplicates = function(nums) {
return [...new Set(nums)]
};
console.log(removeDuplicates([1,1,2]))
console.log(removeDuplicates([1,1,2,3]))
1
No need to doarray#reduce()
...
– Yosvel Quintero
Nov 22 '18 at 7:05
Yes @YosvelQuintero just realised that. :)
– Nitish Narang
Nov 22 '18 at 7:06
1
Sets can take arrays of inputs directly
– Nitish Narang
Nov 22 '18 at 7:06
1
Good to help.. Removing my answer because yours was first
– Yosvel Quintero
Nov 22 '18 at 7:09
1
That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)
– Nitish Narang
Nov 22 '18 at 7:10
|
show 1 more comment
You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N)
.
Perhaps JS has some high-order function like Python groupby
to make shorter code, but described method is definitely the best possible from algorithmical point of view.
ideone
var removeDuplicates = function(nums) {
let res = ;
let last = NaN
for(i=0; i<nums.length; i++) {
if(nums[i] != last) {
res.push(nums[i]);
last = nums[i];
}
}
return res.length;
};
let arr = [1, 1, 2]
print(removeDuplicates(arr))
>>2
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%2f53425420%2fleetcode-remove-duplicates-from-sorted-array-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try changing includes
to indexOf
, may be includes
is not working in your environment. Also, instead of returning length
you should return res
.
Just in case you want to try another approach, you can look at Sets
like below
var removeDuplicates = function(nums) {
return [...new Set(nums)]
};
console.log(removeDuplicates([1,1,2]))
console.log(removeDuplicates([1,1,2,3]))
1
No need to doarray#reduce()
...
– Yosvel Quintero
Nov 22 '18 at 7:05
Yes @YosvelQuintero just realised that. :)
– Nitish Narang
Nov 22 '18 at 7:06
1
Sets can take arrays of inputs directly
– Nitish Narang
Nov 22 '18 at 7:06
1
Good to help.. Removing my answer because yours was first
– Yosvel Quintero
Nov 22 '18 at 7:09
1
That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)
– Nitish Narang
Nov 22 '18 at 7:10
|
show 1 more comment
You can try changing includes
to indexOf
, may be includes
is not working in your environment. Also, instead of returning length
you should return res
.
Just in case you want to try another approach, you can look at Sets
like below
var removeDuplicates = function(nums) {
return [...new Set(nums)]
};
console.log(removeDuplicates([1,1,2]))
console.log(removeDuplicates([1,1,2,3]))
1
No need to doarray#reduce()
...
– Yosvel Quintero
Nov 22 '18 at 7:05
Yes @YosvelQuintero just realised that. :)
– Nitish Narang
Nov 22 '18 at 7:06
1
Sets can take arrays of inputs directly
– Nitish Narang
Nov 22 '18 at 7:06
1
Good to help.. Removing my answer because yours was first
– Yosvel Quintero
Nov 22 '18 at 7:09
1
That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)
– Nitish Narang
Nov 22 '18 at 7:10
|
show 1 more comment
You can try changing includes
to indexOf
, may be includes
is not working in your environment. Also, instead of returning length
you should return res
.
Just in case you want to try another approach, you can look at Sets
like below
var removeDuplicates = function(nums) {
return [...new Set(nums)]
};
console.log(removeDuplicates([1,1,2]))
console.log(removeDuplicates([1,1,2,3]))
You can try changing includes
to indexOf
, may be includes
is not working in your environment. Also, instead of returning length
you should return res
.
Just in case you want to try another approach, you can look at Sets
like below
var removeDuplicates = function(nums) {
return [...new Set(nums)]
};
console.log(removeDuplicates([1,1,2]))
console.log(removeDuplicates([1,1,2,3]))
var removeDuplicates = function(nums) {
return [...new Set(nums)]
};
console.log(removeDuplicates([1,1,2]))
console.log(removeDuplicates([1,1,2,3]))
var removeDuplicates = function(nums) {
return [...new Set(nums)]
};
console.log(removeDuplicates([1,1,2]))
console.log(removeDuplicates([1,1,2,3]))
edited Nov 22 '18 at 7:07
answered Nov 22 '18 at 7:04
Nitish NarangNitish Narang
2,948815
2,948815
1
No need to doarray#reduce()
...
– Yosvel Quintero
Nov 22 '18 at 7:05
Yes @YosvelQuintero just realised that. :)
– Nitish Narang
Nov 22 '18 at 7:06
1
Sets can take arrays of inputs directly
– Nitish Narang
Nov 22 '18 at 7:06
1
Good to help.. Removing my answer because yours was first
– Yosvel Quintero
Nov 22 '18 at 7:09
1
That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)
– Nitish Narang
Nov 22 '18 at 7:10
|
show 1 more comment
1
No need to doarray#reduce()
...
– Yosvel Quintero
Nov 22 '18 at 7:05
Yes @YosvelQuintero just realised that. :)
– Nitish Narang
Nov 22 '18 at 7:06
1
Sets can take arrays of inputs directly
– Nitish Narang
Nov 22 '18 at 7:06
1
Good to help.. Removing my answer because yours was first
– Yosvel Quintero
Nov 22 '18 at 7:09
1
That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)
– Nitish Narang
Nov 22 '18 at 7:10
1
1
No need to do
array#reduce()
...– Yosvel Quintero
Nov 22 '18 at 7:05
No need to do
array#reduce()
...– Yosvel Quintero
Nov 22 '18 at 7:05
Yes @YosvelQuintero just realised that. :)
– Nitish Narang
Nov 22 '18 at 7:06
Yes @YosvelQuintero just realised that. :)
– Nitish Narang
Nov 22 '18 at 7:06
1
1
Sets can take arrays of inputs directly
– Nitish Narang
Nov 22 '18 at 7:06
Sets can take arrays of inputs directly
– Nitish Narang
Nov 22 '18 at 7:06
1
1
Good to help.. Removing my answer because yours was first
– Yosvel Quintero
Nov 22 '18 at 7:09
Good to help.. Removing my answer because yours was first
– Yosvel Quintero
Nov 22 '18 at 7:09
1
1
That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)
– Nitish Narang
Nov 22 '18 at 7:10
That's so kind of you @YosvelQuintero. That's why I love SO, people are so willing to promote fellow learners. Thank you :)
– Nitish Narang
Nov 22 '18 at 7:10
|
show 1 more comment
You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N)
.
Perhaps JS has some high-order function like Python groupby
to make shorter code, but described method is definitely the best possible from algorithmical point of view.
ideone
var removeDuplicates = function(nums) {
let res = ;
let last = NaN
for(i=0; i<nums.length; i++) {
if(nums[i] != last) {
res.push(nums[i]);
last = nums[i];
}
}
return res.length;
};
let arr = [1, 1, 2]
print(removeDuplicates(arr))
>>2
add a comment |
You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N)
.
Perhaps JS has some high-order function like Python groupby
to make shorter code, but described method is definitely the best possible from algorithmical point of view.
ideone
var removeDuplicates = function(nums) {
let res = ;
let last = NaN
for(i=0; i<nums.length; i++) {
if(nums[i] != last) {
res.push(nums[i]);
last = nums[i];
}
}
return res.length;
};
let arr = [1, 1, 2]
print(removeDuplicates(arr))
>>2
add a comment |
You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N)
.
Perhaps JS has some high-order function like Python groupby
to make shorter code, but described method is definitely the best possible from algorithmical point of view.
ideone
var removeDuplicates = function(nums) {
let res = ;
let last = NaN
for(i=0; i<nums.length; i++) {
if(nums[i] != last) {
res.push(nums[i]);
last = nums[i];
}
}
return res.length;
};
let arr = [1, 1, 2]
print(removeDuplicates(arr))
>>2
You don't use sortness properly. Algorithmically it is more effective to compare item with previous one, so complexity is O(N)
.
Perhaps JS has some high-order function like Python groupby
to make shorter code, but described method is definitely the best possible from algorithmical point of view.
ideone
var removeDuplicates = function(nums) {
let res = ;
let last = NaN
for(i=0; i<nums.length; i++) {
if(nums[i] != last) {
res.push(nums[i]);
last = nums[i];
}
}
return res.length;
};
let arr = [1, 1, 2]
print(removeDuplicates(arr))
>>2
edited Nov 22 '18 at 8:05
answered Nov 22 '18 at 7:14
MBoMBo
47.3k22949
47.3k22949
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%2f53425420%2fleetcode-remove-duplicates-from-sorted-array-javascript%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
have you tried using If(res.indexOf(num) < 0) ? instead of if(res.includes(num) === false)
– Venkatesh Konatham
Nov 22 '18 at 7:01