Remove Duplicates from Sorted Array II

1
2
3
4
5
6
7
8
9
class Solution(object):
    def removeDuplicates(self, nums):
        k = 0

        for i in nums:
            if k < 2 or i != nums[k - 2]:
                nums[k] = i
                k += 1
        return k       

Problem Classification

Language Agnostic Coding Drills

  1. Understanding Variables: The concept of variables is foundational in programming. In this code, variables are used to store the array length (k), and individual elements in the array (i).

  2. Understanding Arrays/Lists: Arrays (or lists) are fundamental data structures in many programming languages. They are used to store multiple values in a single variable. In this code, nums is an array.

  3. Using Loops: Loops are used to repeat a set of statements a certain number of times. Here, a for loop is used to traverse all elements in the array nums.

  4. Conditional Statements: Conditional statements like if are used to execute different blocks of code based on certain conditions. In this code, an if statement is used to check whether the current element does not match either of the two previous indexes.

  5. Array Indexing: Array indexing is used to access or modify the elements of an array. In this code, nums[k - 2] and nums[k] are examples of array indexing.

  6. In-place Array Modifications: This code demonstrates a technique often used to avoid creating extra space, especially when the input is large. The nums array is modified in-place, with the unique elements being moved to the beginning of the array.

  7. Array Traversal: The code demonstrates a technique for traversing an array from start to end, processing each element.

  8. Return Statements: This code includes a return statement, which is used to send a result back to the caller of a function or method. Here, k is returned, which represents the length of the array after duplicates have been removed.

In terms of difficulty, here is the order:

  1. Understanding Variables
  2. Understanding Arrays/Lists
  3. Using Loops
  4. Conditional Statements
  5. Array Indexing
  6. Return Statements
  7. Array Traversal
  8. In-place Array Modifications

Targeted Drills in Python

  1. Understanding Variables:
1
2
3
4
5
# Create two variables and assign them different values. 
# Then, print their sum.
x = 5
y = 10
print(x + y)
  1. Understanding Arrays/Lists:
1
2
3
# Create a list of numbers and print it.
numbers = [1, 2, 3, 4, 5]
print(numbers)
  1. Using Loops:
1
2
3
4
# Use a for loop to print each element in a list.
numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(num)
  1. Conditional Statements:
1
2
3
4
5
# Use an if statement to check if a number is greater than 5.
# If it is, print a message.
number = 7
if number > 5:
    print("Number is greater than 5")
  1. Array Indexing:
1
2
3
# Create a list of numbers and print the element at the second index.
numbers = [1, 2, 3, 4, 5]
print(numbers[1])  # remember, indexing starts at 0
  1. Return Statements:
1
2
3
4
# Write a function that accepts two numbers and returns their sum.
def sum_two_numbers(a, b):
    return a + b
print(sum_two_numbers(3, 4))
  1. Array Traversal:
1
2
3
4
5
# Write a function that accepts a list and prints each element in the list.
def print_elements(lst):
    for element in lst:
        print(element)
print_elements([1, 2, 3, 4, 5])
  1. In-place Array Modifications:
1
2
3
4
5
6
7
# Write a function that accepts a list and an index, 
# and changes the element at that index to 0.
def zero_out_element(lst, idx):
    lst[idx] = 0
numbers = [1, 2, 3, 4, 5]
zero_out_element(numbers, 2)
print(numbers)  # should print [1, 2, 0, 4, 5]