leetcode.com/problems/remove-duplicates-from-sorted-array/

 

Remove Duplicates from Sorted Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

문제를 보면 길이에 따라서 output int를 이용하여 nums array를 읽는 것을 확인 할 수 있다. 

    class Solution {
        public int removeDuplicates(int[] nums) {
            if (nums.length == 0) {
                return 0;
            }

            int numberCount = 1;

            for (int index = 0; index < nums.length - 1; index++) {
                if (nums[index] != nums[index + 1]) {
                    numberCount++;
                }
            }

            return numberCount;
        }
    }

 

+ Recent posts