Skip to content

Commit 2084335

Browse files
authored
I added Intersection of two arrays (CodingWallah#13)
* I added Intersection of two arrays * I added the solution of remove all occurence of given number
1 parent 2ff45c6 commit 2084335

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
## Solution of Intersection Of Two Arrays
3+
4+
#### Time Complexity - O (n*m)
5+
6+
#### Space Complexity - O(n)
7+
8+
```java
9+
10+
class Solution {
11+
public int[] intersection(int[] nums1, int[] nums2) {
12+
HashSet<Integer> hs = new HashSet<>();
13+
for (int i = 0; i <nums1.length ; i++) {
14+
for (int j = 0; j <nums2.length ; j++) {
15+
16+
if(nums1[i]==nums2[j]){
17+
18+
hs.add(nums2[j]);
19+
}
20+
}
21+
}
22+
23+
int []nums3 = new int[hs.size()];
24+
int h=0;
25+
for(Integer n: hs){
26+
nums3[h++]=n;
27+
}
28+
return nums3;
29+
30+
31+
32+
}
33+
}
34+
35+
36+
```
37+
38+
### Solution 2: Two Pointer Approach
39+
40+
#### Time Complexity - O (nlog(n))
41+
42+
#### Space Complexity - O(n)
43+
44+
```java
45+
46+
class Solution {
47+
public int[] intersection(int[] nums1, int[] nums2) {
48+
HashSet<Integer> hs = new HashSet<>();
49+
Arrays.sort(nums1);
50+
Arrays.sort(nums2);
51+
int i=0;
52+
int j=0;
53+
while(i<nums1.length&&j<nums2.length){
54+
if(nums1[i]==nums2[j]){
55+
hs.add(nums1[i]);
56+
i++;j++;
57+
}
58+
else{
59+
if(nums1[i]<nums2[j])i++;
60+
else j++;
61+
}
62+
63+
}
64+
65+
int []nums3 = new int[hs.size()];
66+
int h=0;
67+
for(Integer n: hs){
68+
nums3[h++]=n;
69+
}
70+
return nums3;
71+
72+
73+
74+
}
75+
}
76+
77+
```
78+
79+
<div align="right">
80+
Contributed By <a href="https://github.com/Love-Garg-19"> Love Garg</a>
81+
</div>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Solution of Remove all occurrences of Given Number
2+
3+
#### Time Complexity - O (n)
4+
5+
#### Space Complexity - O(1)
6+
7+
```java
8+
9+
class Solution {
10+
public int removeElement(int[] nums, int val) {
11+
12+
int i = 0;
13+
int n = nums.length;
14+
while (i < n) {
15+
if (nums[i] == val) {
16+
nums[i] = nums[n - 1];
17+
n--;
18+
} else {
19+
i++;
20+
}
21+
}
22+
return n;
23+
}
24+
}
25+
26+
```
27+
<div align="right">
28+
Contributed By <a href="https://github.com/Love-Garg-19"> Love Garg</a>
29+
</div>

0 commit comments

Comments
 (0)