remove duplicates from the sorted array
leetcode
class Solution {
public int removeDuplicates(int[] arr) {
int i=0;
for(int j=1; j<arr.length; j++){
if(arr[i]!=arr[j]){
i++;
arr[i]=arr[j];
}
}
return i+1;
}
}
//GFG
class Solution {
// Function to remove duplicates from the given array.
ArrayList<Integer> removeDuplicates(int[] arr) {
ArrayList<Integer> list = new ArrayList<>();
if (arr.length == 0) return list;
int i = 0;
list.add(arr[i]); // add first element
for (int j = 1; j < arr.length; j++) {
if (arr[j] != arr[i]) {
list.add(arr[j]);
i = j;
}
}
return list;
}
}
.png)
Comments
Post a Comment