public void ArrayAlgorithms_FindCombinationSuchThatPHValueIsNeutralized_Test() { // Arrange var input = new PhValue[] { new PhValue() { Element = "CA", Value = -3 }, new PhValue() { Element = "AR", Value = 3 }, new PhValue() { Element = "MN", Value = 6 }, new PhValue() { Element = "PD", Value = 8 } }; // Act ArrayAlgorithms.FindCombinationSuchThatPHValueIsNeutralized(input); }
/// <summary> /// An array is given with element name and their respective pH values. Print the combination of 2 elements which make a neutral compound /// </summary> /// <param name="input"></param> public static void FindCombinationSuchThatPHValueIsNeutralized(PhValue[] input) { var map = new Dictionary<int, string>(); foreach (var phValue in input) { if (!map.ContainsKey(-phValue.Value)) { map.Add(phValue.Value, phValue.Element); } else { Console.WriteLine(phValue.Element + "," + map[-phValue.Value]); break; } } }