Answers for "remove duplicates from sorted array"

3

remove duplicates from array

let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];

console.log(uniqueChars);

output:
[ 'A', 'B', 'C' ]
Posted by: Guest on November-26-2020
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;
}
Posted by: Guest on June-18-2020
0

remove duplicates in sorted array python

# function for removing duplicates
def removeDuplicate(arr, n):
    j = 0
    
    # traverse elements of arr
    for i in range(0, n-1): 
        # if ith element is not equal to (i+1)th element, then store ith value in arr[j]
        if (arr[i] != arr[i+1]):
            arr[j] = arr[i]
            j = j+1

    # store last value of arr in arr[j]
    arr[j] = arr[n-1]
    j = j+1
    
    # print first j elements of array arr
    for i in range(0, j):
        print("%d"%(arr[i]), end = " ")

arr = [1, 3, 5, 5, 7, 9]
n = len(arr)
# calling function when number of elements in array is greater than 1
if (n > 1):
    removeDuplicate(arr, n)
Posted by: Guest on December-11-2020
0

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)
Posted by: Guest on December-29-2020
0

remove duplicates from sorted array

def remove_duplicate(nums: [int]) -> int:
  nums[:] = sorted(set(nums))
  return len(nums)
Posted by: Guest on December-29-2020
0

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]
Posted by: Guest on December-11-2020

Code answers related to "remove duplicates from sorted array"

Python Answers by Framework

Browse Popular Code Answers by Language