Problem Statement

Pattern: Pattern Ghost Array Related: 80. Remove Duplicates from Sorted Array II 27. Remove Element


Solution

public int removeDuplicates(int[] nums) {
	// init index
	int index =0;
	// get next element array
	for (int num : nums) {
		// if first el of array or el > last el inserted -> insert element
		if(index == 0 || num > nums[index-1]) nums[index++] = num;
	}
	return index;
}

Notes

  • Only add to result array if next element is larger than last element
  • A naive appraoch to this problem is to manaully shift elements forward everytime an element gets repeated.
    • dw i did that too. find on intellij