LeetCode Problem: Remove Duplicates from Sorted Array II

Problem statement:

You are given an integer array nums sorted in increasing order, your task is to remove the duplicates in place such that every element appears at most twice.

Example:

Input:

nums = [1,1,1,2,2,3]

Expected output:

nums1 = [1,1,2,2,3,_]

If you would like to solve the problem on Leetcode, here is the link to the problem: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii

Golang Solution:
func removeDuplicates(nums []int) int {
    
    if len(nums) <= 1 {
        return len(nums)
    }

    countMap := make(map[int]int, 0)

    i, j := 0, 0
    for i < len(nums) && j < len(nums) {
        if res, ok := countMap[nums[j]]; ok {
            if res < 2 {
                countMap[nums[j]] += 1
                nums[i] = nums[j]
                i += 1
            }
        } else {
            countMap[nums[j]] = 1
            nums[i] = nums[j]
            i += 1
        }
        j += 1
    }

    return i
}