Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Gemstones problem solution

YASH PAL, 31 July 202430 November 2025

In this HackerRank Gemstones problem you have Given a list of minerals embedded in each of the rocks, display the number of types of gemstones in the collection.

HackerRank Genstones problem solution

Problem solution in Python programming.

import fileinput

stones = []
gems = []

for line in fileinput.input():
    if fileinput.lineno() == 1:
        lengths = int(line)
    else:
        stones.append(line.strip())

#print(stones)
for x in range(0, len(stones[0])):
    if stones[0][x] not in gems:
        gems.append(stones[0][x])
        for y in range(0, lengths):
            if stones[0][x] not in stones[y]:
                #print(x)
                try :
                    gems.remove(stones[0][x])
                except ValueError:
                    continue

print(len(gems))

Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    
    static int gemStones(ArrayList<String> array)
    {
    	String firstString = array.get(0);
    	
    	// charset with all unique chars
    	HashSet<String> charSet = new HashSet<String>();
    	
    	for (String c : firstString.split(""))
    	{
    		charSet.add(c);
    	}
    	
    	int total = 0;
    	for (String c : charSet)
    	{
    		Boolean exists = true;
    		for (int i = 1 ; i < array.size() ; i++)
    		{
    			String thisLine = array.get(i);
    			if (!thisLine.contains(c))
    			{
    				exists = false;
    			}
    		}
    		
    		if (exists)
    			total++;
    	}
    	
    	return total;
    }

   
 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int count = in.nextInt();
        // escape the n
        in.nextLine();
        ArrayList <String> arrayList = new ArrayList<String>(count);
        
        for (int i=0;i<count;i++)
        {
        	String line = in.nextLine();
        	arrayList.add(line);
        }
        
        in.close();
        
        int result = gemStones(arrayList);
        System.out.println(result);
        
   }
}

Problem solution in C++ programming.

#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define DB(x) cerr << x << " "
#define DBN(x) cerr << #x << "=" << x << endl
#define DBL cerr << endl
#define sz(c) ((int)(c).size())
#define pb push_back
#define mp make_pair
#define endl 'n'

typedef long long int64;

using namespace std;

int n;
int cnt[26];

int main() {
  while (scanf("%d", &n) == 1) {
    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < n; ++i) {
      char s[110];
      scanf("%s", s);
      set<int> was;
      for (int i = 0; s[i] != 0; ++i)
        if (!was.count(s[i])) {
          ++cnt[s[i] - 'a'];
          was.insert(s[i]);
        }
    }
    int res = 0;
    for (int i = 0; i < 26; ++i)
      if (cnt[i] == n) ++res;
    printf("%dn", res);
  }
  return 0;
}

Problem solution in C programming.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

#define NL ('z' - 'a' + 1)

int main(int argc, char *argv[])
{
    if (argc > 1)
        freopen(argv[1], "r", stdin);

    //Helpers for input/output
    int N;
    int total[NL] = {0};
    int part[NL];
    unsigned char elem[101];
    int result = 0;
   
    scanf("%dn", &N);
    for(int i=0; i<N; i++)
    {
        memset(part, 0, NL * sizeof(int));
        scanf("%sn", elem);
        for (unsigned char *c = elem; *c; c++)
        {
            if (!part[*c - 'a'])
            {
                part[*c - 'a']++;
                total[*c - 'a']++;
            }
        }
    }
   
    for(int i=0; i<NL; i++)
    {
        if (total[i] == N)
            result++;
    }
    
    printf("%dn", result);
    return 0;
}

Problem solution in JavaScript programming.

'use strict';

function processData(input) {
    var lines = input.split('n').slice(1);
    var gemElements = lines.reduce(function(prev, curr, index, array) {
        var found = [];
        var letters = curr.split('').map(function(letter){
            if(prev.indexOf(letter) !== -1 && found.indexOf(letter) === -1) {
                found.push(letter);
                return letter; 
            }
        });
        return letters.join('');
    });
    console.log(gemElements.length);
}

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 AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes