0

I have this professor that wants us to make a using the method header Public static int[] eliminateDuplicates(int[] arr) The program uses a randomly generated array to see if there are duplications but the user gives a limit on how many indexes's the random array has. Here is what I have but it's not working.

import java.util.Random;
import java.util.Scanner;
public class Dublicates
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Random generator = new Random();

        System.out.println("Enter array length: ");
        int[] a = new int[input.nextInt()];

        for (int i = 0; i<a.length; i++)
        {
            a[i] = generator.nextInt(a.length*2);
        }
        int[] result = eliminateDuplicates(a);
        System.out.println("The new numbers are: " + result.length);
        System.out.println("The double numbers were:");
        for (int b : result)
        {
            System.out.println(b + " ");
        }
    }
    public static int[] eliminateDuplicates(int[] arr)
    {
        int[] temp = new int[arr.length];
        int size = 0;
        for (int i = 0; i < arr.length; i++) {
            if (linearSearch(temp, arr[i]) == -1) {
                temp[size] = arr[i];
                size++;
            }
            int[] result = new int[size];
            for (int i = 0; i < size; i++) {
                result[i] = temp[i];
            }
            {
                return result;

            }
        }
    }
    public static int linearSearch(int[] arr, int key)
    {
        for(int i = 0; i<arr.length; i++)
        {
            if (key == arr[i])
                return i;
        }
        return -1;
    }
}
3
  • Your return statement is inside your for-loop Commented Nov 5, 2018 at 2:06
  • "but it's not working" means exactly what? What happens and what did you expect to happen? Commented Nov 5, 2018 at 5:02
  • put your inner for loop outside of outer loop Commented Nov 5, 2018 at 5:21

2 Answers 2

1

As your statements making the final result is inside the for loop, the statements inside for will only run once and will not give the right answer.

So you have to change your code as follows.

public static int[] eliminateDuplicates(int[] arr)
{
    int[] temp = new int[arr.length];
    int size = 0;
    for (int i = 0; i < arr.length; i++) {
        if (linearSearch(temp, arr[i]) == -1) {
            temp[size] = arr[i];
            size++;
        }
    }
    int[] result = new int[size];
    for (int i = 0; i < size; i++) {
        result[i] = temp[i];
    }

    return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It will not work because generator.nextInt(a.length*2) can generate 0 and by default array temp is initialised with 0 in its each index. So linearSearch on this array will not work
0

The default values in the integer array is 0, The Random.nextInt() can generate 0 random value, When you run linear search then 0 will not be included in the final resultant array. I have modify Random.nextInt() so that it will not generate 0 random number:

import java.util.Random;
import java.util.Scanner;
public class HelloCodiva
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Random generator = new Random();

        System.out.println("Enter array length: ");
        int[] a = new int[input.nextInt()];      

        for (int i = 0; i<a.length; i++){
            a[i] = generator.nextInt(a.length*2)+1;//So that 0 is not generated
        }

        int[] result = eliminateDuplicates(a);

        for (int originalValue: a){
            System.out.println(originalValue+ " ");
        }

        System.out.println("The new numbers are: " + result.length);
        System.out.println("The double numbers were:");

        for (int b : result){
            System.out.println(b + " ");
        }
}

    public static int[] eliminateDuplicates(int[] arr)
    {
        int[] temp = new int[arr.length];
        int size = 0;
        for (int i = 0; i < arr.length; i++) {
            if (linearSearch(temp, arr[i]) == -1) {
                temp[size] = arr[i];
                size++;
            }
        }

        int[] result = new int[size];       
        System.arraycopy(temp, 0, result, 0, size);
       return result;
    }

    public static int linearSearch(int[] arr, int key)
    {
        for(int i = 0; i<arr.length; i++)
        {
            if (key == arr[i])
                return i;
        }
        return -1;
    }
}

Comments

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.