Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

In-Place Removal of Duplicates Exceeding Two Occurrences in Sorted Arrays

Tech May 18 3

When processing sorted arrays where elements may appear multiple times, enforcing a maximum occurrence limit requires a two-pointer technique. The objective is to compact the sequence so that no value repeats more than twicee, achieving this modification within the original memory bounds with out auxiliary storage.

The core strategy utilizes a read pointer to traverse the dataset and a write pointer to track the boundary of the valid segment. Because the input is pre-sorted, identical values are guaranteed to be adjacent. By comparing the current reading element against the element positioned two steps behind the write pointer, we can determine whether a third consecutive occurence has been reached. If the condition holds, the current element is skipped; otherwise, it advances the write boundary and records the value.

Below is a Go implementation demonstrating this optimized approach:

func filterDuplicateOccurrences(nums []int) int {
	if len(nums) <= 2 {
		return len(nums)
	}

	targetPos := 1
	for scanPos := 2; scanPos < len(nums); scanPos++ {
		if nums[scanPos] != nums[targetPos-2] {
			targetPos++
			nums[targetPos] = nums[scanPos]
		}
	}
	return targetPos + 1
}

Algorithm Breakdown:

  • Initialization: The targetPos marker begins at 1 because the first two positions are automatically preserved.
  • Traversal Loop: The scanPos variable scans forward starting from the third element. At each step, it compares against nums[targetPos-2].
  • Deduplication Logic: If the current value matches the element two slots prior, it indicates a duplicate exceeding the allowed threshold, triggering a silent skip. Otherwise, the marker increments, and the value is committed to the front portion of the slice.
  • Result Calculation: Since indices are zero-based, adding one to the final marker position yields the exact count of retained elements.

Complexity Analysis:

  • Time Complexity: O(N), where N represents the total number of items. Each component traverses the structure exactly once.
  • Space Complexity: O(1), as all operations occur directly on the provided data structure using fixed integer variables.

Execution Walkthrough: Given an initial sequence [0, 0, 1, 1, 1, 1, 2, 3, 3]:

  • Indices 0 and 1 remain untouched (0, 0).
  • Processing index 2 (1): Compares with nums[0] (0). Values differ, so targetPos advances to 2 and stores 1.
  • Processing index 3 (1): Compares with nums[1] (0). Values differ, advances to 3, stores 1. Current valid prefix: [0, 0, 1, 1].
  • Processing index 4 (1): Compares with nums[2] (1). Values match third occurrence. Skipped.
  • Processing index 5 (1): Same check against nums[3] (1). Skipped.
  • Processing index 6 (2): Compares with nums[3] (1). Differs, advances to 4, stores 2.
  • Final valid prefix compacted to [0, 0, 1, 1, 2, 3, 3]. Function returns length 7.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.