Question

Question 4 I40 ptsl Write a program that asks the user to enter 1000 integers to be stored in an array called numbers. Since the same integer might occur (exist in the array multiple times, your program needs to fill a second array, called Isolate that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the element is in the first array). You can use any sorting algorithm you want (You must use at least repeated one function in your program)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

import java.lang.*;
import java.io.*;

class Ideone
{
   public static void main (String[] args) throws java.lang.Exception
   {
  
       Scanner sc= new Scanner(System.in);
      
       System.out.println("Enter number of elements of array:- ");
       num=sc.nextInt();
      
       int[] numbers = new int[num];
       for(int i=0;i<num;i++){
           numbers[i]=sc.nextInt();
       }
      
      
      
List<Integer> isolate = new ArrayList<Integer>();

Boolean dupliceExists;
for (int i = 0; i < numbers.length; i++) {
dupliceExists = Boolean.FALSE;
for (Integer integ : isolate) {
if (Integer.valueOf(numbers[i]).equals(integ)) {
dupliceExists = Boolean.TRUE;
//Get index if you need the index of duplicate from here
}
}
if (!dupliceExists) {
isolate.add(numbers[i]);
}
}

Collections.sort(isolate);

for (int i = 0; i < isolate.size(); i++) {
System.out.println(isolate.get(i));
}
   }
}

here i have created a class in which i am entering the number of elements in array from user (you can enter 1000)

then input the elements of array. then we just find whether the element occurs earlier in the array or not. If it finds in the array and maintains that via boolean variable.

then just added the element in list of interger type and applied the Collections.sort to sort the elements of second array. finally prints them.

Add a comment
Know the answer?
Add Answer to:
Write a program that asks the user to enter 1000 integers to be stored in an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

  • Write a multithreaded sorting program that works as follows: A list of integers is divided into...

    Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice. The two sublists are then merged by a third thread—a merging thread —which merges the two sublists into a single sorted list. Because global data are shared cross all threads, perhaps the easiest way to set up the data...

  • Write a Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

  • LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers...

    LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers and stores them in an array called data. Then call a method that accepts the array as an argument, sorts the array in ascending order and prints out the original AND sorted array (Hint: The method does not return anything in this case, so it should be set to void).

  • Write a console-based program ( C # ) that asks a user to enter a number...

    Write a console-based program ( C # ) that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined...

  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • Exercise 1: Write a C++ program that asks the user to input an integer n followed...

    Exercise 1: Write a C++ program that asks the user to input an integer n followed by n other characters that will be stored in an array. We suppose here that the user will input only lowercase characters between ‘a’ and ‘z’. We also suppose that the user will input different characters. The program will then sort these characters according to an increasing order of their alphabetical order. In this exercise, feel free to use selection sort, insertion sort, or...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Write a program that inputs ten (10) integers and then sorts them in order from largest...

    Write a program that inputs ten (10) integers and then sorts them in order from largest to smallest in the programming language called Perl. You do not need to error check the input; you can assume the user enters integers. You can select the sorting algorithm of your choice, but you must implement this algorithm yourself. You cannot use a built-in sorting function. [15 points] Below is an example of a sample program run: Unsorted: 10, 4, 23, 99, 7,...

  • (C++) Write a function, remove, that takes three parameters: an array of integers, the number of...

    (C++) Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the user to enter 10 digits...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT