L521 Remove Duplicate Numbers in Array
Description
Given an array of integers nums
, logically remove the duplicate elements and return the length of the removed array n
such that the first n
elements of the array nums
contain all the elements of the original array nums
after de-duplication by the de-duplication operation.
You should:
Do it in place in the array.
Put the element after removing the repetition at the beginning of the array.
Return the number of elements after removing duplicate elements.
Wechat reply 【Google】 get the latest requent Interview questions. (wechat id : jiuzhang1104)
You don't need to keep the original order of the integers.
Example
Example 1:
Explanation:
Move duplicate integers to the tail of nums => nums =
[1,3,4,2,?,?]
.Return the number of unique integers in nums =>
4
.
Actually we don't care about what you place in ?
, we only care about the part which has no duplicate integers.
Example 2:
Challenge
Do it in O(n) time complexity.
Do it in O(nlogn) time without extra space.
这题如果允许排序的话跟之前26 Remove Duplicate from Sorted Arra一样。如果不排序的话,可以用个set来去重,还是T:O(n)但多用了O(n)的space。
Last updated