Skip to content

Commit

Permalink
union and intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankush1oo8 committed Mar 16, 2024
1 parent 1431dcd commit 602a705
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Hashing/unionandIntersection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Hashing;

import java.util.*;

public class unionandIntersection {
public static void main(String[] args) {
int arr1[] = { 7, 3, 9 };
int arr2[] = { 6, 3, 9, 2, 9, 4 };
HashSet<Integer> set = new HashSet<>();

// Union
for (int i = 0; i < arr1.length; i++) {
set.add(arr1[i]);
}

for (int i = 0; i < arr2.length; i++) {
set.add(arr2[i]);
}
System.out.println("Union : " + set.size());

// intersection
int count = 0;
set.clear();
for (int i = 0; i < arr1.length; i++) {
set.add(arr1[i]);
}
for (int i = 0; i < arr2.length; i++) {
if (set.contains(arr2[i])) {
count++;
set.remove(arr2[i]);
}
}
System.out.println("Intersection : "+count);
}
}

0 comments on commit 602a705

Please sign in to comment.