Sparse Arrays – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
1 Month Preparation Kit Solutions – HackerRank
C++ Sparse Arrays HackerRank Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
map<string,int> count;
int n;
cin>>n;
while(n--){
string s;
cin>>s;
count[s]++;
}
cin>>n;
while(n--){
string s;
cin>>s;
//cout<<s<<endl;
cout<<count[s]<<endl;
}
return 0;
}
Java Sparse Arrays HackerRank Solution
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
HashMap<String,Integer> hm = new HashMap<>();
for(int i = 0; i < N; ++i) {
String input = sc.next();
if(hm.get(input) == null) hm.put(input,1);
else {
int val = hm.get(input);
hm.put(input,++val);
}
}
int Q = sc.nextInt();
while(Q-->0) {
String query = sc.next();
if(hm.get(query) == null) System.out.println(0);
else System.out.println(hm.get(query));
}
}
}
Python 3 Sparse Arrays HackerRank Solution
n = int(input())
hashmap = {}
for i in range(n):
string = input()
hashmap[string] = 1 if string not in hashmap else hashmap[string] + 1
q = int(input())
for j in range(q):
string = input()
print(0 if string not in hashmap else hashmap[string])
JavaScript Sparse Arrays HackerRank Solution
function processData(input) {
const split = input.split('\n');
const N = parseInt(split[0], 10);
var strings = split.slice(1, N + 1);
const Q = parseInt(split[N+1], 10);
const queries = split.slice(N+2);
const counts = [];
const countMap = new Map();
queries.forEach(query => {
if (countMap.has(query)) {
console.log(countMap.get(query))
} else {
const prevCount = strings.length;
strings = strings.filter(string => string !== query);
const nextCount = strings.length;
const count = prevCount - nextCount;
countMap.set(query, count);
console.log(count)
}
});
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
C Sparse Arrays HackerRank Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int N, Q;
char *N_array[1000], *Q_array[1000];
scanf("%d", &N);
for (int N_i = 0; N_i < N; N_i++) {
char s[21];
scanf("%s", s);
N_array[N_i] = malloc(21);
strcpy(N_array[N_i], s);
}
scanf("%d", &Q);
for (int Q_i = 0; Q_i < Q; Q_i++) {
int occurs = 0, result;
char s[21];
scanf("%s", s);
Q_array[Q_i] = malloc(21);
strcpy(Q_array[Q_i], s);
for (int N_i2 = 0; N_i2 < N; N_i2++) {
result = strcmp(Q_array[Q_i], N_array[N_i2]);
if (result == 0) occurs++;
}
printf("%d\n", occurs);
}
return 0;
}
Leave a comment below