Skip to content

Commit ca34ee7

Browse files
committed
Solution to find Common Elements in two Arrays using HashMap Technique
1 parent a8a87e8 commit ca34ee7

File tree

2 files changed

+121
-39
lines changed

2 files changed

+121
-39
lines changed

src/Hashing/FirstRepeatingElement.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Hashing/HashMapProblems.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package Hashing;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
6+
public class HashMapProblems {
7+
public static void main(String[] args) {
8+
9+
ArrayList<Integer> list = new ArrayList<>();
10+
list.add(10);
11+
list.add(5);
12+
list.add(3);
13+
list.add(4);
14+
list.add(3);
15+
list.add(5);
16+
list.add(6);
17+
18+
int answer = findFirstRepeatingElement(list);
19+
int array [] = {2, 7, 11, 15};
20+
int array2 [] = {2, 2, 11, 15, 3,4,5};
21+
int target = 9;
22+
int [] result = twoSum(array,target);
23+
System.out.println("Array with Indices is " + result[0] + result[1]);
24+
System.out.println("Repeating Element is " + answer);
25+
26+
commonElements(array,array2);
27+
28+
}
29+
30+
static ArrayList<Integer> commonElements(int[] A, int[] B) {
31+
32+
ArrayList<Integer> commonElements = new ArrayList<Integer>;
33+
34+
HashMap<Integer,Integer> hashMap_1 = new HashMap<>();
35+
36+
for (int i = 0; i < A.length; i++){
37+
if (hashMap_1.containsKey(A[i])){
38+
int value = hashMap_1.get(A[i]);
39+
hashMap_1.put(A[i],value++);
40+
}else {
41+
hashMap_1.put(A[i], 1);
42+
}
43+
}
44+
45+
HashMap<Integer,Integer> hashMap_2 = new HashMap<>();
46+
47+
for (int i = 0; i < B.length; i++){
48+
if (hashMap_2.containsKey(B[i])){
49+
int value = hashMap_2.get(B[i]);
50+
hashMap_2.replace(B[i],value+1);
51+
}else {
52+
hashMap_2.put(B[i], 1);
53+
}
54+
}
55+
56+
for (int i = 0; i < A.length; i++){
57+
58+
if (hashMap_2.containsKey(A[0])){
59+
boolean test = false;
60+
for (int element : A) {
61+
if (element == A[0]) {
62+
test = true;
63+
break;
64+
}
65+
}
66+
if (!test){
67+
68+
}
69+
}
70+
71+
}
72+
73+
74+
System.out.println(hashMap_1);
75+
System.out.println(hashMap_2);
76+
77+
return commonElements;
78+
}
79+
80+
static int[] twoSum( int[] A, int B) {
81+
82+
int[] indicesArray = new int[2];
83+
84+
int start = 0; int end = A.length -1;
85+
86+
while (start < end){
87+
88+
if(A[start]+A[end] == B){
89+
indicesArray[0] = start;
90+
indicesArray[1] = end;
91+
92+
return indicesArray;
93+
}
94+
95+
if (A[start]+A[end] > B){
96+
end--;
97+
}else {
98+
start++;
99+
}
100+
}
101+
102+
return indicesArray;
103+
}
104+
105+
static int findFirstRepeatingElement(ArrayList<Integer> arrayList){
106+
107+
int repeatingElement = -1;
108+
109+
HashMap<Integer, Integer> map = new HashMap<>();
110+
111+
for (int i = arrayList.size()-1; i > 0; i--) {
112+
113+
if (map.containsKey(arrayList.get(i))) {
114+
repeatingElement = arrayList.get(i);
115+
}else {
116+
map.put(arrayList.get(i),1);
117+
}
118+
}
119+
return repeatingElement;
120+
}
121+
}

0 commit comments

Comments
 (0)