remove duplicates from unsorted array code example
Example 1: remove duplicates from array
let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];
console.log(uniqueChars);
output:
[ 'A', 'B', 'C' ]
Example 2: remove duplicates array.filter
const uniqueArray = oldArray.filter((item, index, self) => self.indexOf(item) === index);
Example 3: remove duplicates from sorted array
def remove_duplicates(nums: [int]) -> int:
cnt = 1
for index in range(len(nums) - 1):
if nums[index] != nums[index + 1]:
nums[cnt] = nums[index + 1]
cnt += 1
print(cnt)
Example 4: remove duplicates from sorted array
def remove_duplicate(nums: [int]) -> int:
nums[:] = sorted(set(nums))
return len(nums)