class Solution {
public int removeDuplicates(int[] nums) {
ArrayList<Integer> al = new ArrayList<Integer>();
for(int i=0;i<nums.length;i ){
if(!al.contains(i)){
al.add(i);
}
}
for (int i=0; i<al.size(); i ) {
System.out.print(al.get(i));
}
Integer[] arr = new Integer[al.size()];
arr = al.toArray(arr);
return arr;
}
}
I am not understanding where I am going wrong. Below is my error:
Line 16: error: incompatible types: Integer[] cannot be converted to int
return arr;
CodePudding user response:
Instead of using Integer[]
class using the int[]
to create an array and change the code accordingly or else change the return type of function from int[]
to Integer[]
.
Your can change like this,
static int[] your_Func_Name() {
//Your code
}
Or Else,
ArrayList<Integer> al = new ArrayList<Integer>();
for(int i=0;i< nums.length;i ){
if(!al.contains(i)){
al.add(i);
}
}
for(int i=0; i<al.size(); i ){
System.out.print(al.get(i));
}
//Initializing the array using int[] and not Integer[]
int[] arr = new int[al.size()];
//ArrayList al elements are manually copied to array
for (int i = 0; i < arr.length; i ) {
arr[i] = al.get(i);
}
return arr;
CodePudding user response:
Change the return type of your method from public int removeDuplicates(int[] nums)
to public Integer[] removeDuplicates(int[] nums)
The error is due to the return value not matching the return type of the method.