Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java Hashset-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java Hashset- Hacker Rank Solution
Java Hashset- Hacker Rank Solution
Problem :
In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values(Wikipedia). is an example of a set, but is not a set. Today you will learn how to use sets in java by solving this problem.
You are given pairs of strings. Two pairs and are identical if and . That also implies is not same as . After taking each pair as input, you need to print number of unique pairs you currently have.
Complete the code in the editor to solve this problem.
Input Format
In the first line, there will be an integer denoting number of pairs. Each of the next lines will contain two strings seperated by a single space.
Constraints:
- Length of each string is atmost and will consist lower case letters only.
Output Format
Print lines. In the line, print number of unique pairs you have after taking pair as input.
Sample Input
5 john tom john mary john tom mary anna mary anna
Sample Output
1 2 2 3 3
Explanation
- After taking the first input, you have only one pair: (john,tom)
- After taking the second input, you have two pairs: (john, tom) and (john, mary)
- After taking the third input, you still have two unique pairs.
- After taking the fourth input, you have three unique pairs: (john,tom), (john, mary) and (mary, anna)
- After taking the fifth input, you still have three unique pairs.
Java Hashset- Hacker Rank Solution
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); String [] pair_left = new String[t]; String [] pair_right = new String[t]; for (int i = 0; i < t; i++) { pair_left[i] = s.next(); pair_right[i] = s.next(); } HashSet<String> set = new HashSet(t); for (int i = 0; i < t; i++) { set.add(pair_left[i] + " " + pair_right[i]); System.out.println(set.size()); } } }