Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khalilovcmd/44db2e63ff79c7fe684c to your computer and use it in GitHub Desktop.
Save khalilovcmd/44db2e63ff79c7fe684c to your computer and use it in GitHub Desktop.
hacker rank - comparing two lists of numbers (finding the missing numbers in one list)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
// 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
namespace MissingNumbers
{
class Program
{
static void Main(string[] args)
{
int firstListCount = int.Parse(Console.ReadLine());
int[] first = Console.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
Dictionary<int, int> firstTracks = new Dictionary<int, int>();
// find each number (in list A), and it's according number of times repeated in the list (use a dictionary for fast access)
for (int i = 0; i < first.Length; i++)
{
if (firstTracks.ContainsKey(first[i]))
firstTracks[first[i]]++;
else
firstTracks.Add(first[i], 1);
}
// order
firstTracks = firstTracks.OrderBy(a => a.Key).ToDictionary(a => a.Key, a => a.Value);
int secondListCount = int.Parse(Console.ReadLine());
int[] second = Console.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
Dictionary<int, int> secondTracks = new Dictionary<int, int>();
// find each number (in list B), and it's according number of times repeated in the list (use a dictionary for fast access)
for (int i = 0; i < second.Length; i++)
{
if (secondTracks.ContainsKey(second[i]))
secondTracks[second[i]]++;
else
secondTracks.Add(second[i], 1);
}
// order
secondTracks = secondTracks.OrderBy(a => a.Key).ToDictionary(a => a.Key, a => a.Value);
// comparing and finding the missing numbers in one of the lists
foreach (var track in firstTracks)
{
if (track.Value < secondTracks[track.Key])
Console.Write(track.Key + " ");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment