-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathruby.java
More file actions
48 lines (38 loc) · 720 Bytes
/
ruby.java
File metadata and controls
48 lines (38 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Java code to find intersection when
// elements may not be distinct
import java.io.*;
import java.util.Arrays;
class GFG {
// Function to find intersection
static void intersection(int a[], int b[], int n, int m)
{
int i = 0, j = 0;
while (i < n && j < m) {
if (a[i] > b[j]) {
j++;
}
else if (b[j] > a[i]) {
i++;
}
else {
// when both are equal
System.out.print(a[i] + " ");
i++;
j++;
}
}
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 1, 3, 2, 3, 4, 5, 5, 6 };
int b[] = { 3, 3, 5 };
int n = a.length;
int m = b.length;
// sort
Arrays.sort(a);
Arrays.sort(b);
// Function call
intersection(a, b, n, m);
}
}