From 04e3ada6451f65ece302b97010a1c11a1da3e8ac Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 12 Dec 2024 04:33:14 -0600 Subject: [PATCH] adding remove duplicates algo --- .../remove_duplicates_from_sorted_array.py | 18 ++++++++++++++++++ ...ve_duplicates_from_sorted_array_round_12.py | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_12/remove_duplicates_from_sorted_array.py create mode 100644 tests/test_150_questions_round_12/test_remove_duplicates_from_sorted_array_round_12.py diff --git a/src/my_project/interviews/top_150_questions_round_12/remove_duplicates_from_sorted_array.py b/src/my_project/interviews/top_150_questions_round_12/remove_duplicates_from_sorted_array.py new file mode 100644 index 00000000..c16f57c8 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_12/remove_duplicates_from_sorted_array.py @@ -0,0 +1,18 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + + j = 0 + len_nums = len(nums) + + for i in range(len_nums -1): + + if nums[i] != nums[i+1]: + nums[j] = nums[i] + j += 1 + + nums[j] = nums[len_nums - 1] + + return j + 1 diff --git a/tests/test_150_questions_round_12/test_remove_duplicates_from_sorted_array_round_12.py b/tests/test_150_questions_round_12/test_remove_duplicates_from_sorted_array_round_12.py new file mode 100644 index 00000000..7eee0264 --- /dev/null +++ b/tests/test_150_questions_round_12/test_remove_duplicates_from_sorted_array_round_12.py @@ -0,0 +1,13 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_12\ +.remove_duplicates_from_sorted_array import Solution + +class RemoveDuplicatesTestCase(unittest.TestCase): + + def test_remove_duplicates(self): + solution = Solution() + output = solution.removeDuplicates(nums=[1,1,2]) + target = 2 + self.assertEqual(output, target) + + \ No newline at end of file