1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
butalik [34]
3 years ago
8

Find a duplicate (10 points). Write a program FindDuplicate.java that reads n integer arguments from the command line into an in

teger array of length n, where each value is between is 1 and n, and displays true if there are any duplicate values, false otherwise.
Computers and Technology
2 answers:
mestny [16]3 years ago
7 0

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

 int n;

 Scanner input = new Scanner(System.in);

 System.out.print("Enter the array size: ");

 n = input.nextInt();

 

 int[] numberArray = new int[n];

 

 System.out.print("Enter the numbers between 1 and " + n + ":");

 

 for(int i=0; i<n; i++){

     numberArray[i] = input.nextInt();

 }

 

 System.out.println(findDublicate(numberArray, n));

}

public static boolean findDublicate(int[] numberArray, int n){

    boolean flag = false;

    for (int i = 0; i < n; i++) {

        for (int j = i + 1 ; j < n; j++) {

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

                 flag = true;

                 break;

            }

        }

    }

    return flag;

}

}

Explanation:

<u>Inside the function:</u>

- Check if there are duplicates by comparing each number in the array using nested for loop

<u>Inside the main:</u>

- Ask the user for array size and the numbers

- Put them into the array called <em>numberArray</em>

- Call the function and print the result

Galina-37 [17]3 years ago
3 0

Answer:

import java.util.Arrays;

import java.util.Random;

public class FindDuplicate {

   public static void main(String[] args) {

       // Declare an array of size n

       // assume n=10

       int n=10;

       boolean duplicate =true;

       int [] intArray = new int[n];

       Random rand = new Random();

       //Reading n values into the array using a for loop

       for(int i=1; i<n; i++){

           intArray[i]= rand.nextInt(11);

       }

       System.out.println(Arrays.toString(intArray));

       for(int i = 0; i < n; i++) {

           for(int j = i + 1; j < n; j++) {

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

                   duplicate = true;

break;

           }

     }

  }

       if(duplicate){

           System.out.println("True");

       }

       else{

           System.out.println("False");

       }

   }

}

Explanation:

Using Java programming language

An Array is declared of size n =10

Using the Random class random integers from 1-10 inclusive are generated and loaded into the array (using a for loop)

 To find duplicate elements, two for loops are used. The first loop (outer) iterates through the array and selects an element, The inner loop is used for comparison of the selected element with other elements in the array.

If there is a match, the boolean variable duplicate will be set to true

And True is printed to the screen, else False is printed

You might be interested in
What does it mean for a computing system to have an interface? A. An interface is a conduit through which the user can interact
Wittaler [7]

Answer:

An interface device (IDF) is a hardware component or system of components that allows a human being to interact with a computer, a telephone system, ...

Explanation:

5 0
1 year ago
lenny joggy along a trail for 1.35 hours he ran at a pace of 3.2 miles per hour. how far did lenny jog?
Dmitriy789 [7]

Answer:

Lenny jogged along a trail for 1.35 hours. He ran at a pace of 3.2 miles per hour. How far did Lenny jog? Answer Lenny jogged 4.32 miles.

Explanation:

4 0
2 years ago
How to download gta5 ​
Kazeer [188]
Buy the game and hit download
8 0
3 years ago
Read 2 more answers
An employee has been writing a secure shell around software used to secure executable files. The employee has conducted the appr
Marat540 [252]

Since the employee has been writing a secure shell around software used to secure executable files and he has also conducted a self-test, he seems to be working in the development environment.

C. Development

<u>Explanation:</u>

The development environment requires an employee to work with algorithms and programming techniques to make programs/software's that are further passed on to the testing department to ensure that they are ready to be launched or deployed.

Although it is the job of testing environment and testers to conduct tests on the developed software's/programs, it is always a good practice to run some self-tests to ensure the efficiency and accountability of a program/software.

3 0
3 years ago
The main difference between local storage and session storage is
amid [387]

Answer:

local storage is stored indefinitely; session storage is lost when the browser closes

Explanation:

The difference between local and session storage is that the session storage expires when the browser closes while the local storage is stored until someone else or the user deletes it.Local storage is there indefinitely.

5 0
3 years ago
Other questions:
  • The Spell Checker can be found in which two locations in most word processing software?
    11·1 answer
  • To hide gridline when you display or print a worksheet
    14·1 answer
  • As the demand for goods and services decreases, job growth.
    14·2 answers
  • Your friend wants to purchase a new hard drive. She wants a drive that has fast access because she will use it to edit videos on
    8·1 answer
  • How do you import an SVG file?
    12·1 answer
  • Which type of digital picture is made by strong individual pixel value
    11·2 answers
  • What color and hat do you choose in Among Us
    15·1 answer
  • Animations<br> Animations are !<br> Blank which can be Applied to blank in a presentation?
    7·1 answer
  • 5. Why do we need programming language?​
    13·2 answers
  • One of the difficult things about working in game design is that while there are many different roles, most of them only match o
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!