Counting Sort 1

Published on August 06, 2021
Last updated August 06, 2021

Problem description

Given a list of integers, count and return the number of times each value appears as an array of integers.

Input format

  • int n: Number of elements
  • arr[n]: an array of integers

Output

  • int[100]: a frequency array

Link to the problem on hackerrank

Solution in java

import java.util.*;

class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int arr[] = new int[100];
        for (int i = 0; i < n; i++) {
            int temp = sc.nextInt();
            arr[temp]++;
        }
        for (int i = 0; i < 100; i++) {
            System.out.print(arr[i] + " ");
        }
        sc.close();
    }
}


Tags :