In place solution to remove duplicates from a sorted listFinding longest common prefixRemove duplicates from...
Customer Requests (Sometimes) Drive Me Bonkers!
How to Reset Passwords on Multiple Websites Easily?
Flow chart document symbol
Inappropriate reference requests from Journal reviewers
How can I get through very long and very dry, but also very useful technical documents when learning a new tool?
Why not increase contact surface when reentering the atmosphere?
Large drywall patch supports
Is HostGator storing my password in plaintext?
Avoiding estate tax by giving multiple gifts
How to create a 32-bit integer from eight (8) 4-bit integers?
What does "I’d sit this one out, Cap," imply or mean in the context?
How do we know the LHC results are robust?
What does 算不上 mean in 算不上太美好的日子?
Applicability of Single Responsibility Principle
Energy of the particles in the particle accelerator
How do scammers retract money, while you can’t?
Is expanding the research of a group into machine learning as a PhD student risky?
Number of words that can be made using all the letters of the word W, if Os as well as Is are separated is?
Magento 2 custom phtml file not calling to all products view pages
Where does the Z80 processor start executing from?
How can a function with a hole (removable discontinuity) equal a function with no hole?
I'm in charge of equipment buying but no one's ever happy with what I choose. How to fix this?
Failed to fetch jessie backports repository
How can I quit an app using Terminal?
In place solution to remove duplicates from a sorted list
Finding longest common prefixRemove duplicates from csv based on conditionsCount of Smaller Numbers After SelfRemove all values duplicates from listFind a number which equals to the total number of integers greater than itself in an arrayFirstDuplicate FinderGiven a sorted array nums, remove the duplicates in-placeHash table solution to twoSumA One-Pass Hash Table Solution to twoSummerge two sorted list in a decent solution but get low scores
$begingroup$
I am working on the problem removeDuplicatesFromSortedList
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
My solution and TestCase
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
"""
"""
#Base Case
if len(nums) < 2: return len(nums)
#iteraton Case
i = 0 #slow-run pointer
for j in range(1, len(nums)):
if nums[j] == nums[i]:
continue
if nums[j] != nums[i]: #capture the result
i += 1
nums[i] = nums[j] #in place overriden
return i + 1
class MyCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_raw1(self):
nums = [1, 1, 2]
check = self.solution.removeDuplicates(nums)
answer = 2
self.assertEqual(check, answer)
def test_raw2(self):
nums = [0,0,1,1,1,2,2,3,3,4]
check = self.solution.removeDuplicates(nums)
answer = 5
self.assertEqual(check, answer)
unittest.main()
Run but get a report
Runtime: 72 ms, faster than 49.32% of Python3 online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 14.8 MB, less than 5.43% of Python3 online submissions for Remove Duplicates from Sorted Array.
Less than 5.43%, I employ the in-place strategies but get such a low rank, how could improve it?
python
New contributor
$endgroup$
add a comment |
$begingroup$
I am working on the problem removeDuplicatesFromSortedList
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
My solution and TestCase
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
"""
"""
#Base Case
if len(nums) < 2: return len(nums)
#iteraton Case
i = 0 #slow-run pointer
for j in range(1, len(nums)):
if nums[j] == nums[i]:
continue
if nums[j] != nums[i]: #capture the result
i += 1
nums[i] = nums[j] #in place overriden
return i + 1
class MyCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_raw1(self):
nums = [1, 1, 2]
check = self.solution.removeDuplicates(nums)
answer = 2
self.assertEqual(check, answer)
def test_raw2(self):
nums = [0,0,1,1,1,2,2,3,3,4]
check = self.solution.removeDuplicates(nums)
answer = 5
self.assertEqual(check, answer)
unittest.main()
Run but get a report
Runtime: 72 ms, faster than 49.32% of Python3 online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 14.8 MB, less than 5.43% of Python3 online submissions for Remove Duplicates from Sorted Array.
Less than 5.43%, I employ the in-place strategies but get such a low rank, how could improve it?
python
New contributor
$endgroup$
add a comment |
$begingroup$
I am working on the problem removeDuplicatesFromSortedList
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
My solution and TestCase
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
"""
"""
#Base Case
if len(nums) < 2: return len(nums)
#iteraton Case
i = 0 #slow-run pointer
for j in range(1, len(nums)):
if nums[j] == nums[i]:
continue
if nums[j] != nums[i]: #capture the result
i += 1
nums[i] = nums[j] #in place overriden
return i + 1
class MyCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_raw1(self):
nums = [1, 1, 2]
check = self.solution.removeDuplicates(nums)
answer = 2
self.assertEqual(check, answer)
def test_raw2(self):
nums = [0,0,1,1,1,2,2,3,3,4]
check = self.solution.removeDuplicates(nums)
answer = 5
self.assertEqual(check, answer)
unittest.main()
Run but get a report
Runtime: 72 ms, faster than 49.32% of Python3 online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 14.8 MB, less than 5.43% of Python3 online submissions for Remove Duplicates from Sorted Array.
Less than 5.43%, I employ the in-place strategies but get such a low rank, how could improve it?
python
New contributor
$endgroup$
I am working on the problem removeDuplicatesFromSortedList
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
My solution and TestCase
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
"""
"""
#Base Case
if len(nums) < 2: return len(nums)
#iteraton Case
i = 0 #slow-run pointer
for j in range(1, len(nums)):
if nums[j] == nums[i]:
continue
if nums[j] != nums[i]: #capture the result
i += 1
nums[i] = nums[j] #in place overriden
return i + 1
class MyCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_raw1(self):
nums = [1, 1, 2]
check = self.solution.removeDuplicates(nums)
answer = 2
self.assertEqual(check, answer)
def test_raw2(self):
nums = [0,0,1,1,1,2,2,3,3,4]
check = self.solution.removeDuplicates(nums)
answer = 5
self.assertEqual(check, answer)
unittest.main()
Run but get a report
Runtime: 72 ms, faster than 49.32% of Python3 online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 14.8 MB, less than 5.43% of Python3 online submissions for Remove Duplicates from Sorted Array.
Less than 5.43%, I employ the in-place strategies but get such a low rank, how could improve it?
python
python
New contributor
New contributor
New contributor
asked 2 mins ago
AliceAlice
1794
1794
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Alice is a new contributor. Be nice, and check out our Code of Conduct.
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%2fcodereview.stackexchange.com%2fquestions%2f216403%2fin-place-solution-to-remove-duplicates-from-a-sorted-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Alice is a new contributor. Be nice, and check out our Code of Conduct.
Alice is a new contributor. Be nice, and check out our Code of Conduct.
Alice is a new contributor. Be nice, and check out our Code of Conduct.
Alice is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f216403%2fin-place-solution-to-remove-duplicates-from-a-sorted-list%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