Udemy: Master Coing Interview - Hash Tables - #78

Run Settings
LanguageC#
Language Version
Run Command
using System; using System.Collections.Generic; class MainClass { // Given an array, return the first recurring value // Example input: {2, 5, 1, 2, 3, 5, 1, 2, 4} should return 2 // Example input: {2, 1, 1, 2, 3, 4, 1, 2, 4} should return 1 // Example input: {2, 3, 4, 5} should return undefined (-1) static void Main() { Console.WriteLine("Hello World!"); int[] array1 = {2, 5, 1, 2, 3, 5, 1, 2, 4}; int[] array2 = {2, 1, 1, 2, 3, 4, 1, 2, 4}; int[] array3 = {2, 3, 4, 5}; Console.WriteLine("First recurring value in array1 is " + FirstRecurring(array1)); Console.WriteLine("First recurring value in array2 is " + FirstRecurring(array2)); Console.WriteLine("First recurring value in array3 is " + FirstRecurring(array3)); } public static int FirstRecurring(int[] array) { Dictionary<int, int> dict = new Dictionary<int, int>(); for (int i = 0; i < array.Length; i++) { if (dict.ContainsKey(array[i])) { return array[i]; } dict.Add(array[i], 1); } return -1; } }
Editor Settings
Theme
Key bindings
Full width
Lines