Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Intuitive way of solution.

Thai Minh

Sep 17, 2023

class Solution: def remove(self, arr): # TODO: Write your code here # non_dup_ptr to track non duplicate values # another is to scan our current value array # two cases: non duplicate and duplicate # non duplicate: not equal and it next to each other, increment non_duplicate # duplicate: keep increment duplicate until find no duplicate, increa non-duplicate and replace it value if len(arr) < 2: return 1 non_dup_ptr = 0 curr_ptr = 1 while curr_ptr < len(arr): if arr[non_dup_ptr] != arr[curr_ptr]: if curr_ptr > non_dup_ptr + 1: # we need to swap non_dup_ptr += 1 arr[non_dup_ptr] = arr[curr_ptr] else: non_dup_ptr += 1 curr_ptr += 1 return non_dup_ptr + 1

0

0

Comments
Comments

On this page

Problem Statement

Try it yourself