Write a program FindDuplicate.java that reads n integer arguments from the command line into an integer array of length n, where each value is between is 1 and n, and displays true if there are any duplicate values, false otherwise.

Respuesta :

Answer:

  1. public class FindDuplicate{
  2.    public static void main(String[] args) {
  3.        Scanner input = new Scanner(System.in);
  4.        int n = 5;
  5.        int arr[] = new int[n];
  6.        for(int i=0; i < arr.length; i++){
  7.            int inputNum = input.nextInt();
  8.            if(inputNum >=1 && inputNum <=n) {
  9.                arr[i] = inputNum;
  10.            }
  11.        }
  12.        for(int j =0; j < arr.length; j++){
  13.            for(int k = 0; k < arr.length; k++){
  14.                if(j == k){
  15.                    continue;
  16.                }else{
  17.                    if(arr[j] == arr[k]){
  18.                        System.out.println("True");
  19.                        return;
  20.                    }
  21.                }
  22.            }
  23.        }
  24.        System.out.println("False");
  25.    }
  26. }

Explanation:

Firstly, create a Scanner object to get user input (Line 4).

Next, create an array with n-size (Line 7) and then create a for-loop to get user repeatedly enter an integer and assign the input value to the array (Line 9 - 14).

Next, create a double layer for-loop to check the each element in the array against the other elements to see if there is any duplication detected and display "True" (Line 21 - 22). If duplication is found the program will display True and terminate the whole program using return (Line 23). The condition set in Line 18 is to ensure the comparison is not between the same element.

If all the elements in the array are unique the if block (Line 21 - 23) won't run and it will proceed to Line 28 to display message "False".

The program illustrates the use of arrays, loops and conditions.

The program in Java is as follows, where comments are used to explain some lines.

import java.util.*;

public class FindDuplicate{

  public static void main(String[] args) {

//This creates the scanner object

      Scanner scnr = new Scanner(System.in);

//This declares n and userInput

      int n, userInput;

//This gets input for n

      n = scnr.nextInt();

//This declares the array

      int myArr[] = new int[n];

//This iterates through the array

      for(int i=0; i < myArr.length; i++){

//This gets input for each element of the array

          userInput = scnr.nextInt();

//This while loop ensures that the input is between 1 and n

          while(userInput <1 || userInput >n) {

           userInput = scnr.nextInt();

          }

//This inserts userInput to the array

          myArr[i] = userInput;    

      }

//This iterates through the array      

      for(int i =0; i < myArr.length; i++){

//This iterates through every other elements of the array

          for(int j = 0; j < myArr.length; j++){

//This checks if the array positions are different

              if(i != j){

// This checks for duplicates

              if(myArr[i] == myArr[j]){

//If yes, this prints true

                      System.out.println("True");

//And the loop is exited

                      return;

                  }

          }

          }

      }

//This prints false, if there are no duplicates

      System.out.println("False");

  }

}

See attachment for sample run

Read more about arrays, loops and conditions at:

https://brainly.com/question/20396087

Ver imagen MrRoyal