Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a java program for Intersection of two Integer arrays #17

Merged
merged 3 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
48 changes: 48 additions & 0 deletions Intersection of two Integer arrays/arrayintersection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//by Siddharth Saurabh (https://github.com/siddhartthecoder)
import java.util.*;

public class arrayintersection {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("enter number of elements in first array");

int n = sc.nextInt();

int arr1[] = new int[n];

System.out.println("enter elements");

for (int i = 0; i < n; i++) { //for reading array 1
arr1[i] = sc.nextInt();

}

System.out.println("enter number of elements in second array");

int m = sc.nextInt();

int arr2[] = new int[m];

System.out.println("enter elements");

for (int i = 0; i < m; i++) { //for reading array 2
arr2[i] = sc.nextInt();

}
System.out.println("The intersection is :");
//Intersection logic
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
System.out.print(" " + arr2[j] + " ");
}
}
}
System.out.println("");

}

}