Eureka Problem
Remove Duplicates from Sorted Array
Easy
2 implementations
Categories
Templates
package com.eureka
package array.recursive
import scala.annotation.tailrec
object RemoveDuplicatesFromSortedArray:
def removeDuplicates(nums: Array[Int]): Int =
@tailrec def loop(left: Int, right: Int): Int =
if right == nums.length then left + 1
else if nums(left) == nums(right) then loop(left, right + 1)
else
nums(left + 1) = nums(right)
loop(left + 1, right + 1)
loop(0, 0)
#include <algorithm>
#include <vector>
class RemoveDuplicatesFromSortedArray {
public:
[[nodiscard]] constexpr int removeDuplicates(std::vector<int>& nums) const noexcept {
if (nums.empty()) [[unlikely]]
return 0;
auto result = std::ranges::unique(nums);
return static_cast<int>(result.begin() - nums.begin());
}
};