Missing Numbers HackerRank Solution

In this Missing Numbers HackerRank solution, we have Given two arrays of integers, find which elements in the second array are missing from the first array.

Example

The  array is the orginal list. The numbers missing are .

Notes

  • If a number occurs multiple times in the lists, you must ensure that the frequency of that number in both lists is the same. If that is not the case, then it is also a missing number.
  • Return the missing numbers sorted ascending.
  • Only include a missing number once, even if it is missing multiple times.
  • The difference between the maximum and minimum numbers in the original list is less than or equal to .

Function Description

Complete the missingNumbers function in the editor below. It should return a sorted array of missing numbers.

missingNumbers has the following parameter(s):

  • int arr[n]: the array with missing numbers
  • int brr[m]: the original array of numbers

Returns

  • int[]: an array of integers

Input Format

There will be four lines of input:

 – the size of the first list, 
The next line contains  space-separated integers 
 – the size of the second list, 
The next line contains  space-separated integers 

Constraints

Sample Input

10
203 204 205 206 207 208 203 204 205 206
13
203 204 204 205 206 207 205 208 203 206 205 206 204

Sample Output

204 205 206

Explanation

 is present in both arrays. Its frequency in  is , while its frequency in  is . Similarly,  and  occur twice in , but three times in . The rest of the numbers have the same frequencies in both lists.

Missing Numbers HackerRank Solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in C#

public static List<int> missingNumbers(List<int> arr, List<int> brr)
    {
        arr.Sort();
        brr.Sort();
        var sortedSet = new SortedSet<int>();
        var j = 0;
        for (int i = 0;i<brr.Count;i++)
        {
            if (j<arr.Count&&brr[i] == arr[j])
            {
                 j++;
            }
            else
            {
                sortedSet.Add(brr[i]);
            }
        }
        return sortedSet.ToList();
    }

Problem Solution in Python

def missingNumbers(arr, brr):
    # Write your code here
    ans=[]
    for i in range(len(brr)):
        if brr[i] not in arr:
            ans.append(brr[i])
        else:
            arr.remove(brr[i])
    return sorted(list(set(ans)))

Problem Solution in JavaScript

function missingNumbers(arr, brr) {
    let missing = [];
    
    //sort both
    arr = arr.sort((a, b) => a-b);
    brr = brr.sort((a, b) => a-b);
    
    // run through, if missing, push to new array
    let arri = 0;
    for (let i = 0; i < brr.length; i++) {
        if (brr[i] != arr[arri]) {
            if (missing.indexOf(brr[i]) == -1) {
                missing.push(brr[i]);
            }
            continue;
        }
        arri++;
    }
    return missing
}
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment