/*
 * Algorithm / 演算法：
 *   Same fast/slow two-pointer scan as the C version.
 *   Invariant: nums[0..k-1] always contains the unique values seen so far, in sorted order.
 *   不變量：nums[0..k-1] 永遠存放目前已遇到的所有唯一值，且仍維持排序。
 */

#include <vector>   // std::vector：動態陣列容器 / dynamic array container from the STL
using namespace std;

class Solution {
public:
    // LeetCode 簽章：傳入 vector<int>& （by reference 表示直接修改原陣列，不是複製）。
    // LeetCode signature: vector<int>& means "pass by reference" — we mutate the caller's array.
    int removeDuplicates(vector<int>& nums) {
        // .empty() 是 vector 的成員函式，O(1) 判斷是否為空。
        // .empty() is a vector member function returning true iff size() == 0; O(1).
        if (nums.empty()) return 0;

        // k：慢指針，下一個寫入位置；同時等於目前唯一元素的數量。
        // k: slow pointer / next write index / count of uniques so far.
        // 用 size_t（無號整數型別）以匹配 vector::size() 的回傳型別，避免有號/無號比較警告。
        // Use size_t to match vector::size()'s return type and avoid signed/unsigned warnings.
        size_t k = 1;

        // 範圍限制 for 迴圈：i 從 1 跑到 nums.size() - 1。
        // Classic indexed for-loop: i goes from 1 up to nums.size() - 1.
        for (size_t i = 1; i < nums.size(); ++i) {
            // 比較目前元素和最後一個已保留的唯一值。
            // Compare current element with the last kept unique value.
            if (nums[i] != nums[k - 1]) {
                // 發現新值，寫入並前進寫入指針。
                // New unique value found — write it and advance the write pointer.
                nums[k] = nums[i];
                ++k;  // 前置 ++ 在這裡和後置 ++ 等效，但前置習慣上略快（雖然對 int 沒差）。
                      // Pre-increment is idiomatic in C++ (matters for iterators, not for int).
            }
            // 否則是重複，略過。/ Otherwise it's a duplicate — skip.
        }

        // 回傳值要轉回 int 以符合題目簽章 / Cast back to int to match the required return type.
        return static_cast<int>(k);
    }
};
