I have this problem where I have two people willing to sell some object, but only if they have the same object.
First they say how many objects they would like to sell in order. Example: 3 3 - which indicates that they both would like to sell 3 objects each, whereafter they indicate the number on their objects. If any matches are found there's a counter incrementing. At last the terminal closes iff. the last two integers are two 0's.
Sample input: (I've made some spacing between the two, to make it more clear as to which the integers belong to)
3 3
1
2
3
1
2
4
0 0
Sample output:
2
Another sample input:
5 3
1
2
3
4
5
1
2
3
0 0
'another' Sample Output:
3
This is my code:
import java.util.*;
public class CD {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
int testCases1 = sc.nextInt();
HashSet<Integer> set = new HashSet<Integer>();
HashSet<Integer> set1 = new HashSet<Integer>();
for (int i = 0; i < testCases; i++) {
set.add(sc.nextInt());
}
for (int i = 0; i < testCases1; i++) {
set1.add(sc.nextInt());
}
int counter = 0;
if (set.size() > set1.size() || set.size() == set1.size()) {
for (Integer x : set1) {
if (set.contains(x)) {
counter++;
}
}
}else{
for (Integer x : set) {
if (set1.contains(x)) {
counter++;
}
}
}
int ending = sc.nextInt();
int ending1 = sc.nextInt();
if ((ending == 0 && ending1 == 0)) {
System.out.println(counter);
sc.close();
}
}
}
Also with the sample input my code does fine, but it's not marked as correct with other types of input. Any ideas, suggestions etc? Would love to hear. Best Regards.
EDIT: There can not be any duplicates as the question explicitly says that each person only sells one as a max of each object. So person 1 for instance can't sell object '1' more than once.
Second Edit: I've been notified that another question may have the answer the other similar question. Yet this is somewhat different.