Skip to content

Commit 2fecf50

Browse files
committed
finding all subsets array question
1 parent 2ddce2c commit 2fecf50

File tree

10 files changed

+153
-0
lines changed

10 files changed

+153
-0
lines changed

Collections/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.cdac.collections.map;
2+
3+
import java.util.HashMap;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
public class HashMapDemo {
9+
public static void main(String[] args) {
10+
HashMap<Integer, String> map = new HashMap<>();
11+
//ADD
12+
map.put(1, "One");
13+
map.put(2, "Two");
14+
map.put(3, "Three");
15+
map.put(null, null);
16+
map.put(null, "Hello");
17+
map.put(4, "Hello");
18+
//only one null key allowed
19+
//duplicate keys are not allowed
20+
21+
//CHECK OR CONTAIN
22+
boolean keyAvailable = map.containsKey(11);
23+
System.out.println("Is Key 11 is available :: "+keyAvailable);
24+
keyAvailable = map.containsKey(1);
25+
System.out.println("Is Key 1 is available :: "+keyAvailable);
26+
boolean valueAvailable = map.containsValue("Five");
27+
System.out.println("Is value Five available :: "+valueAvailable);
28+
29+
//READ OR ITERATE
30+
String value = map.get(2);
31+
System.out.println("Value for the Key 2 is :: "+value);
32+
value = map.get(1);
33+
System.out.println("Value for the Key 1 is :: "+value);
34+
35+
//SIZE AND ISEMPTY
36+
int mapSize = map.size();
37+
boolean isMapEmpty = map.isEmpty();
38+
System.out.println("Map Size is :: "+mapSize);
39+
System.out.println("Is the Map is empty :: "+isMapEmpty);
40+
41+
//READ ALL THE KEYS
42+
System.out.println("Read All the Keys - ");
43+
Set<Integer> keySet = map.keySet();
44+
Iterator<Integer> keyIterator = keySet.iterator();
45+
while(keyIterator.hasNext()){
46+
Integer key = keyIterator.next();
47+
System.out.println(key);
48+
}
49+
50+
//READ ALL THE VALUES
51+
System.out.println("Read all the values - ");
52+
List<String> listValues = (List<String>) map.values();
53+
for(String val : listValues){
54+
System.out.println(val);
55+
}
56+
57+
58+
}
59+
}

FileIO/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

Generics/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

InterviewPrograms/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.java.array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
6+
//MAXIMUM SCORE IS 100
7+
public class FindDuplicates {
8+
public static void main(String[] args) {
9+
HashSet<Integer> set = new HashSet<>();
10+
int array[] = {4,2,4,5,2,3,1,2,3};
11+
for(int i =0;i<array.length;i++){
12+
if(array[Math.abs(array[i])] > 0)
13+
array[Math.abs(array[i])] = -array[Math.abs(array[i])];
14+
else{
15+
set.add(Math.abs(array[i]));
16+
}
17+
}
18+
System.out.println("No of duplicate elements are :: "+set.size());
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.java.array;
2+
3+
/*
4+
In the given array, only one element is not repeated
5+
all other elements are repeated exactly 2 times.
6+
Find the element which occured only one time.
7+
*/
8+
public class FindUniqueElt {
9+
public static void main(String[] args) {
10+
int array[] = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5};
11+
int output = array[0];
12+
for(int i=1;i<array.length;i++)
13+
output ^= array[i];
14+
System.out.println(output);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.java.array;
2+
3+
public class FindingAllSubsets {
4+
5+
public static void main(String[] args) {
6+
//solution 1 follows bitwise approach
7+
// solution1();
8+
9+
//solution 2 follows recursion approach
10+
solution2();
11+
}
12+
13+
public static void solution1() {
14+
char set[] = {'a','b','c','d'};
15+
int n = set.length;
16+
for (int i = 0; i < (1 << n); i++) {
17+
System.out.print("{ ");
18+
for (int j = 0; j < n; j++){
19+
if ((i & (1 << j)) > 0)
20+
System.out.print(set[j] + " ");
21+
}
22+
System.out.println("}");
23+
}
24+
}
25+
26+
public static void solution2(){
27+
int array[] = {10,20,30,40,50};
28+
int visited[] = new int[array.length];
29+
traverse(array, visited,0);
30+
}
31+
32+
public static void traverse(int array[],int visited[],int curIndex){
33+
System.out.print("\n{ ");
34+
for(int i=0;i<visited.length;i++)
35+
if(visited[i] == 1)
36+
System.out.print(array[i]+" ");
37+
System.out.print("}");
38+
39+
for(int j=curIndex;j<array.length;j++){
40+
visited[j] = 1;
41+
traverse(array, visited,j+1);
42+
visited[j] = 0;
43+
}
44+
}
45+
}
46+
47+
/*
48+
*
49+
* Input: S = {a, b, c, d} Output: {}, {a} , {b}, {c}, {d}, {a,b}, {a,c}, {a,d},
50+
* {b,c}, {b,d}, {c,d}, {a,b,c}, {a,b,d}, {a,c,d}, {b,c,d}, {a,b,c,d}
51+
*
52+
*/

Networking/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

Threads/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

0 commit comments

Comments
 (0)