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 program in Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
Write a Java program that reads two integers on the keyboard and displays them on the...
Write a Java program that reads two integers on the keyboard and displays them on the screen.
a)     Write a program that reads a list of integers, and outputs all even integers in the...
a)     Write a program that reads a list of integers, and outputs all even integers in the list. For example, if the input list is [1, 2, 13, 14, 25], the output list should be [2, 14]. b)    Based on your code for part (a), write a program that reads a list of integers, and reports True if all numbers in the list are even, and False otherwise. Example: For input [1, 2, 13, 14, 25], the output should be False. Your...
1) Using either emacs or vi write a C program that reads 100 integers from stdin...
1) Using either emacs or vi write a C program that reads 100 integers from stdin into a 2-dimensional 10x10 array. The program will read one additional integer which is used to multiply each element of the array. The final array should be printed to stdout as follows: Run 1: Enter 100 integers: 1 2 3 4 5 6 7 8 9 10 11 …. 100 Enter factor: 3 Result: 3 6 9 12 … 30 33 36 39 42...
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.
you are to write a program in Java, that reads in a set of descriptions of...
you are to write a program in Java, that reads in a set of descriptions of various geometric shapes, calculates the areas and circumferences of the shapes, and then prints out the list of shapes and their areas in sorted order from smallest to largest area. There are four possible shapes: Circle, Square, Rectangle, and Triangle. The last is always an equilateral triangle. The program should read from standard input and write to standard output. The program should read until...
Write a Java program that reads a name and displays on the screen.
Write a Java program that reads a name and displays on the screen.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT