Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my JAVA code to remove duplicates from an array but I am getting the error mentioned below.

Duplicate.java:24: error: incompatible types: int cannot be converted to int[]
return j;
^
Duplicate.java:36: error: cannot find symbol
Duplicate.removingdupliactes(a,n);
^
symbol: method removingdupliactes(int[],int)
location: class Duplicate
2 errors

What I have tried:

Java
import java.util.*;
public class Duplicate
{
public static int[] removingduplicates(int[] a, int n)
{
if(n==0 || n==1)
{
return a;
}
int temp[] = new int[n];
int j=0;
for(int i=0; i<n-1; i++)
{
if(a[i]!=a[i+1])
{
temp[j++] = a[i];
}
}
temp[j++] = a[n-1];
for(int i=0; i<j; i++)
{
a[i] = temp[i];
}
return j;
}
public static void main(String args[])
{
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int a[] = new int[n];
for(int i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
Duplicate.removingdupliactes(a,n);
}
}
Posted
Updated 22-Apr-21 19:02pm

1 solution

You have declared removingduplicates as returning an array of integers:
Java
public static int[] removingduplicates(int[] a, int n)
But you then declare j as a single integer value, and try to return it:
Java
public class Duplicate
    {
    public static int[] removingduplicates(int[] a, int n)
        {
...
        int j=0;
...
        return j;
        }
    ...
    }

Probably, what you wanted to return was temp instead of j?
 
Share this answer
 
Comments
chaitanya chitturi 23-Apr-21 1:32am    
yes you are right I need to return temp only thanks
chaitanya chitturi 23-Apr-21 1:44am    
what is wrong with my second error I am still getting that.
OriginalGriff 23-Apr-21 1:59am    
What do you think is wrong with it?
I'm not trying to be funny, or difficult here - but that's a very, very simple error which you should be able to spot and fix for yourself.
I'll give you a hint: look at the line that java is reporting the error on (the message tells you the line number is "36" and almost certainly your editor will go directly there with CTRL+G; or you could double click the error in your IDE and that may take you right there). Then look at the actual error message "cannot find symbol" which tells you a lot, and compare the call line with the definition of the method it calls ...
It should be pretty obvious what the problem is!
OriginalGriff 23-Apr-21 2:03am    
What do you think the problem is?
Look at the error message: "cannot find symbol". What does that tell you? Something is missing, right?
The error also tells you which line it found a problem with: "36" so go to that line (CTRL+G in most editors, double click the error message in most IDEs).
Compare that to the method definition. What's different?

I'm not trying to be funny, or difficult here: this is the type of error that you should be able to spot almost instantly, it doesn't need any real thinking about at all, and it's something you are going to get wrong often - we all do - so it's worth fixing for yourself as it'll save you a heck of a lot of time! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900