Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Counting Sort 1 problem solution

YASH PAL, 31 July 2024

In this HackerRank Counting Sort 1 problem, you have given a list of integers, count and return the number of times each value appears as an array of integers.

HackerRank Counting Sort 1 problem solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

n = int(input())
ar = list(map(int, input().split()))

tot = [0]*100

for j in range(0,n):
    temp = ar[j]
    tot[temp] += 1
print(*tot, sep =' ')    

Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner( System.in );
        in.nextLine();  // not used
        System.out.println( outputString( count( convertToInts( in.nextLine().split( " " ) ) ) ) );
    }
    
    public static int[] count( int[] ar ) {
        int[] count = new int[100];
        for( int nbr : ar ) {
            count[nbr] += 1;
        }
        return count;
    }
    
    private static String outputString( int[] ar ) {
        StringJoiner joiner = new StringJoiner( " " );
        for( Integer value : ar ) {
            joiner.add( value.toString() );
        }
        return joiner.toString();
    }
    
    private static int[] convertToInts( String[] values ) {
        int[] parsed = new int[values.length];
        for( int i = 0; i < values.length; i++ ) {
            parsed[i] = Integer.valueOf( values[i] );
        }
        return parsed;
    }
}

Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int n, val;
	int A[100];
	for (int i = 0; i < 100; i++) {
		A[i] = 0;
	}
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &val);
		A[val]++;
	}
	printf("%d", A[0]);
	for (int i = 1; i < 100; i++) {printf(" %d", A[i]);}
	printf("n");
	return 0;
}

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    int i,j,n,a[1000001];
    int c[100]={0};
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        c[a[i]]++;
    }
    for(i=0;i<100;i++)
        printf("%d ",c[i]);
    printf("n");
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Problem solution in JavaScript programming.

'use strict';

function processData(input) {
    var lines = input.split('n'),
        len = parseInt(lines[0], 10);

    lines = lines[1].split(" ");

    var A = Array.apply(null, new Array(100)).map(Number.prototype.valueOf, 0);

    for (var i=0; i<len; i++) {
        A[parseInt(lines[i], 10)]++;
    }

    console.log(A.join(" "));
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
var _input = "";
process.stdin.on("data", function (input) { _input += input; });
process.stdin.on("end", function () { processData(_input); });

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes