Skip to content

Problem solving in Java #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/_DataStructures_/Hash Tables/hashsets/ContainsDups.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.*;

class ContainsDups{
public boolean check(int [] nums){
//Create a HashSet
HashSet<Integer> set = new HashSet<Integer>();

//Iterating over the nums arrays
for(int i = 0; i < nums.length; i++){
//Check if the set contains the value
if(set.contains(i)){
return true;
}else{
//Otherwise add the value into the hashset
set.add(i);
}
}
return false;
}
}
41 changes: 41 additions & 0 deletions src/_DataStructures_/Hash Tables/hashsets/HappyNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//Happy Number means to check if the numbers are :) or :(

//Prblem Statement:
/* A happy number is a number defined by the following process: Starting with any positive integer,
replace the number by the sum of the squares of its digits, and repeat the process until the number
equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy numbers.
*/

import java.util.*;

class HappyNumber{
public boolean check(int n){

HashSet<Integer> set = new HashSet<Integer>();

//While if set does not contain any number
while(!set.contains(n)){
set.add(n);

//Recursively update the value
n = number(n);

//Check if n is equal to 1
if(n == 1){
return true;
}
}
return false;
}

//Return the sum value from method number
public int number(int n){
int sum = 0;
while(n > 0){
sum += (n%10) * (n%10);
n = n / 10;
}
return sum;
}
}
29 changes: 29 additions & 0 deletions src/_DataStructures_/Hash Tables/hashsets/Intersection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//Intersection of two arrays
import java.util.*;

class Intersection{
public int[] check(int[] nums1, int[] nums2) {
//Create two hashsets
HashSet<Integer> set1 = new HashSet<Integer>();
for(Integer i : nums1){
set1.add(i);
}

HashSet<Integer> set2 = new HashSet<Integer>();
for(Integer i : nums2){
set2.add(i);
}

//Used the built-in method for intersection of two arrays
set1.retainAll(set2);

//Created a dummy array for output for getting the result of interesection
//between two arrays
int[] output = new int[set1.size()];
int index = 0;
for(Integer o : set1){
output[index++] = o;
}
return output;
}
}
17 changes: 17 additions & 0 deletions src/_DataStructures_/Hash Tables/hashsets/SingleNum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//Used these strategy in most of the test cases
import java.util.*;

class SingleNum{
public int check(int[] nums){

//Initializing the count as zero
int count = 0;

//Iterating over the nums array
for(int i = 0; i < nums.length; i++){
//Using XOR Operator
count ^= nums[i];
}
return count;
}
}