VelizarAng

CountingNumbers

Apr 13th, 2021
803
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class NumberOfNumbers {
  5.     public static void FillingArr(int[] arr){
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         for (int i = 0; i < arr.length; i++) {
  9.             System.out.printf("Enter element [%d]: ", i);
  10.             arr[i] = scan.nextInt();
  11.         }
  12.     }
  13.  
  14.     public static void SortingArr(int[] arr){
  15.         int i, j, Buff;
  16.         int n = arr.length;
  17.         for(i = 0; i < n - 1 ; i++){
  18.             for(j = 0; j < n - i - 1; j++){
  19.                 if(arr[j] > arr[j + 1]){
  20.                     Buff = arr[j];
  21.                     arr[j] = arr[j + 1];
  22.                     arr[j + 1] = Buff;
  23.                 }
  24.             }
  25.         }
  26.     }
  27.  
  28.     public static void CountingNumbers(int[] arr, ArrayList<Integer> arr2){
  29.         int count = 0;
  30.         int number = 0;
  31.         for (int j = 0; j < arr.length; j++) {
  32.             number = arr[j];
  33.             if(!arr2.contains(arr[j])){
  34.                 for (int i = 0; i < arr.length; i++) {
  35.                     if (number == arr[i]) {
  36.                         count++;
  37.                     }
  38.                 }
  39.                 System.out.println(number + " --> " + count + " times");
  40.             }
  41.             arr2.add(number);
  42.             count = 0;
  43.         }
  44.     }
  45.  
  46.     public static void main(String[] args) {
  47.         Scanner scan = new Scanner(System.in);
  48.         ArrayList<Integer> arr2 = new ArrayList<>();
  49.  
  50.         System.out.print("Enter the number of elements: ");
  51.         int n = scan.nextInt();
  52.         int[] arr = new int[n];
  53.  
  54.         FillingArr(arr);
  55.         SortingArr(arr);
  56.         CountingNumbers(arr, arr2);
  57.     }
  58. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×