Write a program that asks the user to enter 1000 integers to be stored in an arr
ID: 3835269 • Letter: W
Question
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)Explanation / Answer
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.