Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions ArraysSolutions/Intersection of two arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

## Solution of Intersection Of Two Arrays

#### Time Complexity - O (n*m)

#### Space Complexity - O(n)

```java

class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> hs = new HashSet<>();
for (int i = 0; i <nums1.length ; i++) {
for (int j = 0; j <nums2.length ; j++) {

if(nums1[i]==nums2[j]){

hs.add(nums2[j]);
}
}
}

int []nums3 = new int[hs.size()];
int h=0;
for(Integer n: hs){
nums3[h++]=n;
}
return nums3;



}
}


```

### Solution 2: Two Pointer Approach

#### Time Complexity - O (nlog(n))

#### Space Complexity - O(n)

```java

class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> hs = new HashSet<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
int i=0;
int j=0;
while(i<nums1.length&&j<nums2.length){
if(nums1[i]==nums2[j]){
hs.add(nums1[i]);
i++;j++;
}
else{
if(nums1[i]<nums2[j])i++;
else j++;
}

}

int []nums3 = new int[hs.size()];
int h=0;
for(Integer n: hs){
nums3[h++]=n;
}
return nums3;



}
}

```

<div align="right">
Contributed By <a href="https://github.com/Love-Garg-19"> Love Garg</a>
</div>
29 changes: 29 additions & 0 deletions ArraysSolutions/Remove all occurrence of Given number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Solution of Remove all occurrences of Given Number

#### Time Complexity - O (n)

#### Space Complexity - O(1)

```java

class Solution {
public int removeElement(int[] nums, int val) {

int i = 0;
int n = nums.length;
while (i < n) {
if (nums[i] == val) {
nums[i] = nums[n - 1];
n--;
} else {
i++;
}
}
return n;
}
}

```
<div align="right">
Contributed By <a href="https://github.com/Love-Garg-19"> Love Garg</a>
</div>