Answer to Question #318801 in Java | JSP | JSF for Wolf1442

Question #318801

Write a program 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 most 7 numbers


The numbers must be output in increasing order for example:



15​40​28​62​62​15​28

1
Expert's answer
2022-03-26T18:45:23-0400
     
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {
        
    public static void main(String[] args) {

        System.out.println("Enter the list:"); 
        Scanner scanner = new Scanner(System.in);
        List<Integer> _inputs = new ArrayList<>();;
        for(int i=0; i<7; i++){ 
               _inputs.add(Math.abs(Integer.parseInt(scanner.nextLine())));
        }
    
        scanner.close();
        
        Set<Integer> uniqueSet = new HashSet<Integer>(_inputs);
        for (Integer temp : uniqueSet) {
                System.out.println(temp + ": " + Collections.frequency(_inputs, temp));
        }

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS