fork download
  1. import java.util.*;
  2.  
  3. class MapReduce {
  4.  
  5. private Map<Integer, HashSet<String>> intermediate = new LinkedHashMap<>();
  6. private Set<Integer> finalResult = new HashSet<>();
  7.  
  8. Set<Integer> execute(int[] R, int[] S) {
  9. for (int aR : R) {
  10. mapper(aR, "R");
  11. }
  12. for (int aS : S) {
  13. mapper(aS, "S");
  14. }
  15.  
  16. Iterator it = intermediate.entrySet().iterator();
  17. while (it.hasNext()) {
  18. Map.Entry<Integer, HashSet<String>> pair = (Map.Entry) it.next();
  19. reducer(pair.getKey(), pair.getValue());
  20. it.remove();
  21. }
  22. return finalResult;
  23. }
  24.  
  25. private void emit(int obj) {
  26. finalResult.add(obj);
  27. }
  28.  
  29. private void reducer(int key, HashSet value) {
  30. // Fill up the question mark in the reducer function!
  31. if (value.size() == 2) {
  32. emit(key);
  33. }
  34. }
  35.  
  36. private void mapper(int key, String value) {
  37. emitIntermediate(key, value);
  38. }
  39.  
  40. private void emitIntermediate(int key, String value) {
  41. if (intermediate.containsKey(key)) {
  42. intermediate.get(key).add(value);
  43. return;
  44. }
  45. intermediate.put(key, new HashSet<String>() {{
  46. add(value);
  47. }});
  48. }
  49. }
  50.  
  51. public class Main {
  52.  
  53. public static void main(String[] arg) {
  54.  
  55. Scanner sc = new Scanner(System.in);
  56. int Nr = sc.nextInt();
  57. int Ns = sc.nextInt();
  58. int[] R = new int[Nr];
  59. int[] S = new int[Ns];
  60. for (int i = 0; i < Nr; i++) {
  61. R[i] = sc.nextInt();
  62. }
  63.  
  64. for (int i = 0; i < Ns; i++) {
  65. S[i] = sc.nextInt();
  66. }
  67.  
  68. MapReduce mapReduceJob = new MapReduce();
  69. Set<Integer> result = mapReduceJob.execute(R, S);
  70.  
  71. result.forEach(System.out::println);
  72. }
  73.  
  74. }
Success #stdin #stdout 0.26s 321408KB
stdin
10 10
-51
-43
74
-96
24
-14
11
77
-45
-90
45
8
29
0
-43
-13
-72
71
-96
-26
stdout
-43
-96