0

Input: 1,4,2,6,7,5,1,2 Output:2

Counting the number of duplicated numbers in a given array for java. I first sorted the array and then counted duplicates. It's showing me error that variable c is not used and that this method should return value of int.

public class Duplicates
public static void main(String[] args) {
    int[]list;
    int[]c;
        int[] c = new int[list.length];
        int temp;
        for (int i = 0; i < list.length - 1; i++) {
            for (int j = i + 1; j < list; j++) {
                if (list[I] > list[j]) {
                    temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                    c = list;
                }
            }
        }
        int n = 0;
        int counter = 0;
        int a = -1;
        for (int i = 0; i < c.length; ++i) {
            if (c[i] == a) {
                ++n;
                if (n == 1) {
                    ++counter;
                    if (counter == 1) {
                        System.out.print(c[i]);
                    } else {
                        System.out.print("," + c[i]);
                    }
                }
            } else {
                a = c[i];
                n = 0;
            }
        }
        System.out.println("\nNumber of Duplicated Numbers in array:" + counter);
    }
}
6
  • "It's showing me error that variable c is not used" -- That's more likely a warning not an error. Commented Jan 27, 2019 at 12:55
  • Do have a look at this. Something similar is already asked [stackoverflow.com/questions/17630727/… Commented Jan 27, 2019 at 12:58
  • 1
    Your method is supposed to be returning an int[], but I can't see where you're doing so. Commented Jan 27, 2019 at 13:02
  • "method should return value of int." - well that's what you promise by the method signature ... to return an array of int values Commented Jan 27, 2019 at 13:05
  • should I return it by adding "return list;" at the end? Commented Jan 27, 2019 at 13:08

1 Answer 1

0

It's showing me error that variable c is not used

This should be a warning. So the code should still run correctly even with this is showing.

this method should return value of int

This is a compilation error and since you are not returning any int array at the end of the method, your method's return type should be void. You should change your method signature as below,

public static void c(int[] list)

Otherwise you will need to return an int array at the end of your method.

After fixing your code,

public class Duplicates {
    public static void main(String[] args) {
        int[] list = new int[]{1, 4, 2, 6, 7, 5, 1, 2};
        int temp;

        for (int i = 0; i < list.length; ++i) {
            for (int j = 1; j < (list.length - i); ++j) {
                if (list[j - 1] > list[j]) {
                    temp = list[j - 1];
                    list[j - 1] = list[j];
                    list[j] = temp;
                }
            }
        }

        int n = 0, counter = 0;
        int previous = -1;
        for (int i = 0; i < list.length; ++i) {
            if (list[i] == previous) {
                ++n;
                if (n == 1) {
                    ++counter;
                    if (counter == 1) {
                        System.out.print(list[i]);
                    } else {
                        System.out.print(", " + list[i]);
                    }
                }
            } else {
                previous = list[i];
                n = 0;
            }
        }

        System.out.println("\nNumber of Duplicated Numbers in array: " + counter);
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

should I return an int array by writing "return list;" after system.out?
It would get rid of the error. But since your method is for finding the count, returning that list would be meaningless. So you can change your method type to void as I have shown or you can change it to int instead of int[] and return counter
Thank you, I changed my method type to void. I was trying to run the program on eclipse, but it says "selection should contain a main type"
Does your class have the main method? public static void main(String[] args) And where are you calling this method?
should I put this instead at the beginning: public static void main(String[] args) { int[]list; int[]c;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.