Leetcode : Remove Duplicates from Sorted Array

Clash Royale CLAN TAG#URR8PPP
Leetcode : Remove Duplicates from Sorted Array
Why this code is not accepted by Leetcode compiler?
My program is running in netbeans. but leetcode compiler is not accepting it.
Here is my code:
import java.util.Arrays;
public class RemoveDuplicateFromArray
public static int removeDuplicates(int nums)
int temp, count = 0 ,l = 0;
temp = nums[0];
for(int i = 1; i < nums.length - (1 + l); i++ )
if(temp == nums[i])
count = count + 1;
for(int j = i; j < nums.length - count; j++)
nums[j] = nums[j+1];
i = i - 1;
else
temp = nums[i];
i = i ;
l = count;
nums = Arrays.copyOfRange(nums, 0, nums.length-count);
if(nums.length == 2)
if(nums[0] == nums[1])
nums = Arrays.copyOfRange(nums, 0, nums.length-1);
return nums;
Here is my main mathod:
public static void main(String args)
int nums = new int 1,1,1; // showing error here.
System.err.println("nums lenght: " +nums.length);
int new_nums = removeDuplicates(nums);
for(int i : new_nums)
System.err.print(i + " ");
The error is :
incompatible types: int cannot be converted to int.
2 Answers
2
I expect leetcode is having trouble with this line:
int new_nums = removeDuplicates(nums);
This isn't the typical way to define an integer array (this is the C style). Java does support the syntax, but it's a little arcane. See this answer for more detail.
Try this instead:
int new_nums = removeDuplicates(nums);
Just tried this on LeetCode, seems to work:
i tried it also. but did not work for me.
– salma209
Aug 10 at 18:28
Tried the code you provided on LeetCode, everything seems ok. Can you provide more information about the error you're getting?
– Michael Powers
Aug 10 at 18:49
I would do it a bit like this myself:
import java.util.ArrayList;
import java.util.List;
public class Main
public static void main(String args)
int nums = new int1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 7, 7, 8, 9, 0, 0, 0, 0;
System.out.println("nums lenght: " + nums.length);
int new_nums = removeDuplicates(nums);
for (int i : new_nums)
System.out.print(i + " ");
public static int removeDuplicates(int nums)
List<Integer> found = new ArrayList();
for (int i = 1; i < nums.length; i++)
if (!found.contains(nums[i]))
found.add(nums[i]);
int ret = new int[found.size()];
for(int i = 0; i < found.size(); i++)
ret[i] = found.get(i);
return ret;
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what version of Java does this Leetcode tool use?
– m.antkowicz
Aug 9 at 20:11