Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Calculate count of every number in array A
and B
. Check every number count from B
if it is in the array A
or its count is lower than the count of the number in the array A
.
I created solution in:
All solutions are also available on my GitHub.
Java
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 |
import java.util.*; import java.util.stream.Collectors; public class MissingNumbers { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int length1 = stdin.nextInt(); Map<Integer, Integer> numHash1 = new HashMap<>(); for(int i = 0; i < length1; i++) { int value = stdin.nextInt(); int count = numHash1.getOrDefault(value, 0); numHash1.put(value, count + 1); } int length2 = stdin.nextInt(); Map<Integer, Integer> numHash2 = new HashMap<>(); for(int i = 0; i < length2; i++) { int value = stdin.nextInt(); int count = numHash2.getOrDefault(value, 0); numHash2.put(value, count + 1); } List<Integer> missingNumbers = new LinkedList<>(); for(int numKey : numHash2.keySet()) { if(numHash1.get(numKey) == null || numHash2.get(numKey) > numHash1.get(numKey)) { missingNumbers.add(numKey); } } Collections.sort(missingNumbers); System.out.println(missingNumbers.stream().map(i -> i.toString()).collect(Collectors.joining(" "))); stdin.close(); } } |
JavaScript
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 |
'use strict'; const processData = input => { let lines = input.split('\n'); let array1 = lines[1].split(' ').map(i => parseInt(i)); let array2 = lines[3].split(' ').map(i => parseInt(i)); let numHash1 = new Map(); let numHash2 = new Map(); for(let i = 0; i < array1.length; i++) { let count = numHash1.get(array1[i]); if(!count) { numHash1.set(array1[i], 1); } else { numHash1.set(array1[i], count + 1); } } for(let i = 0; i < array2.length; i++) { let count = numHash2.get(array2[i]); if(!count) { numHash2.set(array2[i], 1); } else { numHash2.set(array2[i], count + 1); } } let missingNumbers = []; for(let key of numHash2.keys()) { let countB = numHash2.get(key); if(!numHash1.get(key) || countB > numHash1.get(key)) { missingNumbers.push(key); } } console.log(missingNumbers.sort((a, b) => a - b).join(" ")); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import scala.collection.mutable.ListBuffer import scala.io.Source object MissingNumbers extends App { val lines = Source.stdin.getLines().filter(_.contains(" ")).toList val list1 = lines(0).split(" ").map(_.toInt) val list2 = lines(1).split(" ").map(_.toInt) val groupedByNumber1 = list1.groupBy(i => i) val groupedByNumber2 = list2.groupBy(i => i) println(missingNumbers(groupedByNumber1, groupedByNumber2).mkString(" ")) def missingNumbers(a: Map[Int, Array[Int]], b: Map[Int, Array[Int]]): List[Int] = { val result: ListBuffer[Int] = ListBuffer.empty b.keys.foreach(key => { val presentInA = a.get(key).isDefined val amountB = b.get(key).get.length if(!presentInA || amountB > a.get(key).get.length) { result += key } }) result.toList.sorted } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
a_length = gets.chomp.to_i a = gets.chomp.split.map { |e| e.to_i } b_length = gets.chomp.to_i b = gets.chomp.split.map { |e| e.to_i } a_hash = Hash.new(0) b_hash = Hash.new(0) a.each { |e| a_hash[e] += 1} b.each { |e| b_hash[e] += 1} result = [] b_hash.each_key do |key| b_value = b_hash[key] a_value = a_hash[key] result << key if a_value.nil? || b_value > a_value end result.sort! { |i, j| i <=> j } puts result.join(" ") |