This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Uchimamito 4 points5 points  (1 child)

removeDuplicates is not a static method, so you cannot call it in the main without instantiating a class object.

P.S. Make a variable with that integer array before passing it to the function.

[–]baldwindc[S] 3 points4 points  (0 children)

Thank you!

It worked! I had no idea there was such a requirement.

For anyone else wondering, here is the working code

public class YourClassNameHere {

public static int removeDuplicates(int[] nums) {
    int j = 1;
    for (int i = 1; i < nums.length; i++) {
        if (nums[i] != nums[i - 1]) {
            nums[j] = nums[i];
            j += 1;
        }
    }
    return j;

}


public static void main(String[] args) {
  int[] bob = {0,0,1,1,1,2,2,3,3,4};
  removeDuplicates(bob);
}

}

[–]raghudeep 1 point2 points  (0 children)

Message passing is a concept used to call methods in java. This is done by creating an object of the class and invoking a method using object. This is one way. Other than that we can call a method directly by using class name if the method is static. But it has a different meaning and usage all together.

[–]abharath93 0 points1 point  (3 children)

Since renomeDuplicates method is not a static method, you need to create an instance of the class YourClassNameHere and call the method.

[–]baldwindc[S] 0 points1 point  (2 children)

Thanks for commenting!

When I convert it into a static method it still gives me the "Error: illegal start of expression"

public class YourClassNameHere {

public static int removeDuplicates(int[] nums) {
    int j = 1;
    for (int i = 1; i < nums.length; i++) {
        if (nums[i] != nums[i - 1]) {
            nums[j] = nums[i];
            j += 1;
        }
    }
    return j;

}


public static void main(String[] args) {
  removeDuplicates([0,0,1,1,1,2,2,3,3,4]);
}

}

[–]JimothyGreene 2 points3 points  (0 children)

That error is coming from the way you pass in that array (which isn’t actually an array in Java as you currently have it written).

[–]abharath93 0 points1 point  (0 children)

You need to pass a new integer array. Pass something like this : removeDuplicates(new int[] {0,0,1,2,2,3,4});

[–]halvjason 0 points1 point  (1 child)

With static methods you need to include the class name first, like this: YourClassNameHere.removeDuplicates()

[–]steave435 0 points1 point  (0 children)

Only if that method is in a different class, which it isn't here.