0

I am trying to create a random array without duplicates.

The assignment is to take an integer array and maximum value from user, fills the array with random numbers between 0 and maximum value, and display random array without duplicates, WITHOUT using any other classes except random and scanner.

This is a sample output:

Please enter the size of the array: 10

Please enter the maximum value: 50

[39,2,17,49,12,19,40,31,42,15]

I need help in removing the duplicates. I am not sure if what I am doing is correct, I am a bit of a beginner but here is what I have so far. Help would be greatly appreciated. Thanks.

public class Fill {
    private static int size;
    private static int maxVal;

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        // Ask user to enter the size of the array
        System.out.print("Please enter the size of the array: ");
        size = kb.nextInt();

        // Ask user to enter the maximum value allowed

        System.out.print("Please enter the maximum value: ");
        maxVal = kb.nextInt();

        // Call fill() method
        int arr[] = fill(size, maxVal);

        // Print filled array
        System.out.print("[");
        for (int i = 0; i < arr.length - 1; i++)
            System.out.print(arr[i] + ",");
        System.out.print(arr[arr.length - 1] + "]");
    }

    public static int[] fill(int size, int maxVal) {
        int arr[] = new int[size];

        Random random = new Random();

        // Fills the array with random numbers between 0 and maximum value
        if (size <= 0 || maxVal < size - 1) {
            System.out.print("Incorrect Parameters. Please Retry");
            main(null);
        } else {
            for (int j = 0; j < size; j++) {
                arr[j] = random.nextInt(maxVal);
                // Check array for duplicates
                for (int k = j + 1; k < size; k++) {
                    if(arr[j] == arr[k]) {
                        //create new random array
                        fill(size, maxVal);
                    }
                }
            }
        }

        return arr;
    }
}

1 Answers1

1

I have edited and fixed some issues in your code as below:

public class Fill {
    private static int size;
    private static int maxVal;

    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        // Ask user to enter the size of the array
        System.out.print("Please enter the size of the array: ");
        size = kb.nextInt();

        // Ask user to enter the maximum value allowed

        System.out.print("Please enter the maximum value: ");
        maxVal = kb.nextInt();

        // Call fill() method
        int arr[] = fill(size, maxVal);

        // Print filled array
        System.out.print("[");
        for (int i = 0; i < arr.length - 1; i++)
            System.out.print(arr[i] + ",");
        System.out.print(arr[arr.length - 1] + "]");
    }

    public static int[] fill(int size, int maxVal) {
        int arr[] = new int[size];

        Random random = new Random();

        // Fills the array with random numbers between 0 and maximum value
        if (size <= 0 || maxVal < size ) {
            System.out.print("Incorrect Parameters. Please Retry");
            main(null);
        } else {
            for (int j = 0; j < size; j++) {
                int newNumber = random.nextInt(maxVal + 1);
                // Check array for duplicates

                while(alreadyExist(newNumber, arr)){
                    newNumber = random.nextInt(maxVal + 1);
                }
                arr[j] = newNumber;
            }
        }

        return arr;
    }

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

Now it does not return any repetitive value.

Morteza
  • 642
  • 7
  • 17
  • i updated the answer. however, the `ThreadLocalRandom.current().nextInt()` is a JDK 7 specific class, not an external or third-party one. – Morteza Oct 10 '20 at 06:45
  • thank you very much, this works except if I were to answer with the size being the same as maxVal, or maxVal being one less than size, for some reason the array would not print out. I am not sure why this is. – cupcake12345 Oct 10 '20 at 15:32
  • For instance: Please enter the size of the array: 10 Please enter the maximum value: 10 the array does not print Please enter the size of the array: 10 Please enter the maximum value: 9 the array does not print as well when it should print because there are enough elements for it to print without duplicating – cupcake12345 Oct 10 '20 at 15:33
  • @cupcake12345 I updated the answer. Just consider the allowed range in my answer is 1 to `n` (1 and `n` included). To change the boundary numbers (include or exclude min and max numbers), please do some efforts. Also It will not print anything when the size is 10 and maximum value is 9 since there are not enough numbers to fill it. – Morteza Oct 10 '20 at 18:14
  • sorry for my lack of knowledge, i am not very great at java. however, since we are filling the array from 0 to the max value, such as 10, wouldn't there be 10 elements from 0 to 9? I tried to change the boundaries from 0 to n, by doing random.nextInt(maxVal) but it does not print an array. – cupcake12345 Oct 10 '20 at 19:49
  • Hey buddy, you can simply google it for example: java random number range. https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java may help – Morteza Oct 10 '20 at 20:11