diff --git a/src/_DataStructures_/Hash Tables/hashsets/ContainsDups.java b/src/_DataStructures_/Hash Tables/hashsets/ContainsDups.java new file mode 100644 index 00000000..6fc7f2d5 --- /dev/null +++ b/src/_DataStructures_/Hash Tables/hashsets/ContainsDups.java @@ -0,0 +1,20 @@ +import java.util.*; + +class ContainsDups{ + public boolean check(int [] nums){ + //Create a HashSet + HashSet set = new HashSet(); + + //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; + } +} \ No newline at end of file diff --git a/src/_DataStructures_/Hash Tables/hashsets/HappyNumber.java b/src/_DataStructures_/Hash Tables/hashsets/HappyNumber.java new file mode 100644 index 00000000..b159836d --- /dev/null +++ b/src/_DataStructures_/Hash Tables/hashsets/HappyNumber.java @@ -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 set = new HashSet(); + + //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; + } + } \ No newline at end of file diff --git a/src/_DataStructures_/Hash Tables/hashsets/Intersection.java b/src/_DataStructures_/Hash Tables/hashsets/Intersection.java new file mode 100644 index 00000000..7a18d862 --- /dev/null +++ b/src/_DataStructures_/Hash Tables/hashsets/Intersection.java @@ -0,0 +1,29 @@ +//Intersection of two arrays +import java.util.*; + +class Intersection{ + public int[] check(int[] nums1, int[] nums2) { + //Create two hashsets + HashSet set1 = new HashSet(); + for(Integer i : nums1){ + set1.add(i); + } + + HashSet set2 = new HashSet(); + 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; + } +} \ No newline at end of file diff --git a/src/_DataStructures_/Hash Tables/hashsets/SingleNum.java b/src/_DataStructures_/Hash Tables/hashsets/SingleNum.java new file mode 100644 index 00000000..0af1221c --- /dev/null +++ b/src/_DataStructures_/Hash Tables/hashsets/SingleNum.java @@ -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; + } +} \ No newline at end of file