Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit adfdafb

Browse files
committed
Problem solving in Java
1 parent d344e48 commit adfdafb

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.*;
2+
3+
class ContainsDups{
4+
public boolean check(int [] nums){
5+
//Create a HashSet
6+
HashSet<Integer> set = new HashSet<Integer>();
7+
8+
//Iterating over the nums arrays
9+
for(int i = 0; i < nums.length; i++){
10+
//Check if the set contains the value
11+
if(set.contains(i)){
12+
return true;
13+
}else{
14+
//Otherwise add the value into the hashset
15+
set.add(i);
16+
}
17+
}
18+
return false;
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//Happy Number means to check if the numbers are :) or :(
2+
3+
//Prblem Statement:
4+
/* A happy number is a number defined by the following process: Starting with any positive integer,
5+
replace the number by the sum of the squares of its digits, and repeat the process until the number
6+
equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
7+
Those numbers for which this process ends in 1 are happy numbers.
8+
*/
9+
10+
import java.util.*;
11+
12+
class HappyNumber{
13+
public boolean check(int n){
14+
15+
HashSet<Integer> set = new HashSet<Integer>();
16+
17+
//While if set does not contain any number
18+
while(!set.contains(n)){
19+
set.add(n);
20+
21+
//Recursively update the value
22+
n = number(n);
23+
24+
//Check if n is equal to 1
25+
if(n == 1){
26+
return true;
27+
}
28+
}
29+
return false;
30+
}
31+
32+
//Return the sum value from method number
33+
public int number(int n){
34+
int sum = 0;
35+
while(n > 0){
36+
sum += (n%10) * (n%10);
37+
n = n / 10;
38+
}
39+
return sum;
40+
}
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//Intersection of two arrays
2+
import java.util.*;
3+
4+
class Intersection{
5+
public int[] check(int[] nums1, int[] nums2) {
6+
//Create two hashsets
7+
HashSet<Integer> set1 = new HashSet<Integer>();
8+
for(Integer i : nums1){
9+
set1.add(i);
10+
}
11+
12+
HashSet<Integer> set2 = new HashSet<Integer>();
13+
for(Integer i : nums2){
14+
set2.add(i);
15+
}
16+
17+
//Used the built-in method for intersection of two arrays
18+
set1.retainAll(set2);
19+
20+
//Created a dummy array for output for getting the result of interesection
21+
//between two arrays
22+
int[] output = new int[set1.size()];
23+
int index = 0;
24+
for(Integer o : set1){
25+
output[index++] = o;
26+
}
27+
return output;
28+
}
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//Used these strategy in most of the test cases
2+
import java.util.*;
3+
4+
class SingleNum{
5+
public int check(int[] nums){
6+
7+
//Initializing the count as zero
8+
int count = 0;
9+
10+
//Iterating over the nums array
11+
for(int i = 0; i < nums.length; i++){
12+
//Using XOR Operator
13+
count ^= nums[i];
14+
}
15+
return count;
16+
}
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /