remove duplicates from sorted list code example
Example 1: python remove duplicates from list
mylist = ["a", "b", "b", "c", "a"]
mylist = sorted(set(mylist))
print(mylist)
Example 2: python remove duplicates
if mylist:
mylist.sort()
last = mylist[-1]
for i in range(len(mylist)-2, -1, -1):
if last == mylist[i]:
del mylist[i]
else:
last = mylist[i]
mylist = list(set(mylist))
Example 3: remove duplicates from sorted array
// Java
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
Example 4: remove duplicates from a list of lists python
k = [[1, 2], [4], [5, 6, 2], [1, 2], [3], [4]]
new_k = []
for elem in k:
if elem not in new_k:
new_k.append(elem)
k = new_k
print k
Example 5: remove duplicates in sorted array python
j = 0
// traverse elements of arr
for i=0 to n-2
// if ith element is not equal to (i+1)th element of arr, then store ith value in arr[j]
if arr[i] != arr[i+1]
arr[j] = arr[i]
j += 1
// store last value of arr in temp
arr[j] = arr[n-1]
j += 1
// print first j elements of array arr
for i=0 to j-1
print arr[i]
Example 6: 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)