/    Sign up×
Community /Pin to ProfileBookmark

Can’t understand why my algorithm is wrong

Hi folks! I have written an algorithm for “Remove Duplicates from Sorted Array” challenge on leetcode. I ran the code on google dev tools and nothing was wrong. But leetcode says it is. Can somebody point out why is it so? Is it because I didn’t fulfil the condition “you must do this by modifying the input array in-place with O(1) extra memory.”? I haven’t learnt big O notation yet, if so. Below is the code and screenshot:

“`
var removeDuplicates = function(nums) {
for (let a of nums){
if (nums[nums.indexOf(a)] == nums[nums.lastIndexOf(a)] && nums.indexOf(a) !== nums.lastIndexOf(a)) {
nums.splice(nums.indexOf(a), nums.lastIndexOf(a)-nums.indexOf(a)) }
}
return nums;
};
“`

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2020-05-27/1590603266-629820-capture.png]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@NachfolgerMay 27.2020 — ``<i>
</i>uniqueArray = a.filter(function(item, pos) {
return a.indexOf(item) == pos;
})<i>
</i>
``

https://stackoverflow.com/a/9229821/4149581

I encourage you to read the full answer, but it's far too long for me to include.
×

Success!

Help @Hawy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...