Question

In: Computer Science

IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with...

IN java

Create a New Java Project called LastNameDuplicate.

//Given an array of N elements with each element between 1 and N, write a program to determine whether there are any duplicates.

//You must prompt the user for the array elements.

//Display the contents of the array, along with the values that are duplicated and how many times they appeared in the array.

//NOTE: N should be at least 15. Input Validation: Verify that each element entered has a value between 1 and N. If an incorrect value is entered, continuously prompt for a new value. This should not halt or terminate your program.

Solutions

Expert Solution

Java Program:

/* Java Program that reads elements in to array and displays duplicate count */

import java.util.*;

class Duplicates
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
  
       int i, j;
  
       //Reading number of elements
       System.out.print("\n Specify number of elements: ");
       int N = sc.nextInt();

       //Array to hold input elements
       int[] arrayElements = new int[N];
  
       //Reading integers from the console
       System.out.println("\n Enter array elements: ");
  
       for (i = 0; i < N; i++)
       {
           //Loop till user enters a valid value
           do
           {
               //Reading value
               arrayElements[i] = sc.nextInt();
           }while(arrayElements[i] < 1 || arrayElements[i] > N);
       }
      
       //Printing array elements
       System.out.println("\n\n Array Elements: \n");
       for(i=0; i<N; i++)
       {
           System.out.print(" \t " + arrayElements[i]);
       }
      
       int temp;
      
       //Sorting array
       for(i=0; i<N; i++)
       {
           for(j=i+1; j<N; j++)
           {
               //Comparison
               if(arrayElements[i] > arrayElements[j])
               {
                   //Swapping
                   temp = arrayElements[i];
                   arrayElements[i] = arrayElements[j];
                   arrayElements[j] = temp;
               }
           }
       }
      
       boolean duplicate;
       int count;
      
       System.out.println("\n\n");
      
       //Searching for duplicate elements and storing count
       for (i = 0; i < N;)
       {  
           duplicate = false;
           count = 1;
          
           //Searching over next position elements
           for (j = i+1; j < N; j++)
           {
               //If there is no match then break loop
               if(arrayElements[j] != arrayElements[i])
                   break;
                  
               //Comparing elements
               if (arrayElements[i] == arrayElements[j] && j!=i)
               {
                   //Incrementing count
                   count ++;
                   duplicate = true;
               }
           }
          
           //Printing duplicate element
           if(duplicate)
           {
               i = i+count-1;
               System.out.println("\n Element: " + arrayElements[i] + " \t " + count + " times \n");
           }
           else
               i = i+1;
       }
   }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

   


Related Solutions

Java programming language: 1.      Create a new Netbeans project called ArrayLoop. 2.      Declare a fixed length array of...
Java programming language: 1.      Create a new Netbeans project called ArrayLoop. 2.      Declare a fixed length array of integers which can store 8 elements. Do not assign any values. int[] myAddresses = new int[8]; 3.      Create a statement which raises 2 to the power of your loop counter, then subtracts 1, and assigns that value to the corresponding element. For example, when i = 3, 2 to the third power is 8, minus 1 = 7. When i = 4, 2 to the...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
open up a new Java project on Eclipse named Review and create a new class called...
open up a new Java project on Eclipse named Review and create a new class called Review.java. Copy and paste the below starter code into your file: /** * @author * @author * CIS 36B */ //write your two import statements here public class Review {        public static void main(String[] args) { //don't forget IOException         File infile = new File("scores.txt");         //declare scores array         //Use a for or while loop to read in...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain strings             red, orange, yellow, blue, indigo, violet write the statement to insert the String element “pink” to the end of the list. Assume the front of the list is on the left. 2. Outline the basic steps to remove a node from the beginning of a list. Completed answers will be given an immediate upvote :)
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT