-6

Write a static method named repeats_exist(numbers) that takes an array of integers as a parameter and returns true if there are any repeated values in the array, and false if all of the values in the array are unique. Note: you can assume that the array is not empty and the method returns a boolean value.

For example:

Test                                                             Result
System.out.println(repeats_exist(new int[]{2, 3, 4, 5, 6}));     false
System.out.println(repeats_exist(new int[]{2, 3, 4, 3, 6}));     true

Here is my attempt, but still have an error. Please help me to fix the problem.

static boolean repeats_exist(int a[],int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]==a[j])
                return true;
        }
    }
    return false;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 1
    hint: first solve it with paper and pen and then with java. – Jocke Mar 23 '20 at 08:56
  • 2
    So what exactly is the problem? Are you getting an error (if so - which)? The wrong result? – Mureinik Mar 23 '20 at 08:59
  • 3
    Compare the signature of `repeat_exists` in the examples with your actual implementation. How many parameters? See the difference? – Stephen C Mar 23 '20 at 09:01
  • It is a syntax error. Please check the question. – user10380134 Mar 23 '20 at 09:01
  • 4
    *"It is a syntax error. Please check the question"* - I checked. The question does not say that! When you ask for help with debugging some code it is important that you **tell** us what the problem is. Sure. Eventually someone might manage to guess what your errors are ... eventually ... but guessing games are **inefficient**. – Stephen C Mar 23 '20 at 09:02

1 Answers1

0

Change method like this way

static boolean repeats_exist(int a[])
    {
        int i,j;
        for(i=0;i<a.length;i++)
        {
            for(j=i+1;j<a.length;j++)
            {
                if(a[i]==a[j])
                    return true;
            }
        }
        return false;
    }

and the error will disappear

There is only one parameter value in the method offered as Example.

System.out.println(repeats_exist(new int[]{2, 3, 4, 5, 6}));

However, the declared method requires two parameters. Maybe this is the reason for the error.

We need to match the number of parameters for the method declaration and call. enter image description here

Or, when you call a method, you can adjust the number of parameters to get a normal result.

But because you have to put in the value of n directly [it's going to be the length of array a in this code], it's very annoying and it can cause other errors.

public class StackOver
{

    static boolean repeats_exist(int a[], int n)
    {
        int i,j;
        for(i=0;i< n;i++)
        {
            for(j=i+1;j< n;j++)
            {
                if(a[i]==a[j])
                    return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {

        System.out.println(repeats_exist(new int[]{2, 3, 4, 5, 6}, 5));
        System.out.println(repeats_exist(new int[]{2, 3, 4, 3, 6}, 5)); 
    }
 }

If you don't enter the 'n' value correctly, it might not be properly scanned, resulting in incorrect results, or occurring an error (java.lang.ArrayIndexOutOfBoundsException) so I recommend the first method which using the length of the array passed to the parameter.

J4BEZ
  • 374
  • 4
  • 17