Question

In: Computer Science

JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences...

JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences of each with ascending order.

input: line1:number of figures

line2:number

Sample Input

5
-3
100
-1
-2
-1

Sample Output

-3 1
-2 1
-1 2
100 1

Solutions

Expert Solution

Here I am sending code, screens of code and outputs. If you have any questions or queries comment below. Thank you.

Java code:

import java.util.*;
public class Occurence {
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       int size = sc.nextInt(); // reading size from user
       List<Integer> arr = new ArrayList(); // initializing arraylist
      
       for(int i=0 ; i<size ; i++) // loop for adding elements to arraylist
       {
           arr.add(sc.nextInt()); // reading and adding elements to arraylist
       }
       /*treeset arrange the elements in increasing order without repetition*/
       TreeSet<Integer> ts = new TreeSet<Integer>(arr); // initializing treeset
      
       Iterator<Integer> it = ts.iterator(); // creating iterator
      
       while(it.hasNext()) // loop for printing element and occurrence
       {
           int n = it.next();
           System.out.println(n+" "+Collections.frequency(arr,n)); //printing element and occurrence
       }
   }

}

output:


Related Solutions

Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
Write a java program that inserts 25 random integers from 0 to 100 in order into...
Write a java program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
Write a Java program to find the sum of all integers between 200 to 250 which...
Write a Java program to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and...
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 To achieve...
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT