Skip to content

Instantly share code, notes, and snippets.

@Ezeji
Created May 23, 2021 21:09
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 Ezeji/a961dee970ab7b9778216a9d5e15a078 to your computer and use it in GitHub Desktop.
Save Ezeji/a961dee970ab7b9778216a9d5e15a078 to your computer and use it in GitHub Desktop.
This algorithm finds the minimum time difference between any two times in an array.
class Program
{
private static List<string> MinutesArray { get; set; } = new List<string>();
static void Main(string[] args)
{
string[] timeArray = { "16:15", "16:00", "12:20" };
GetMinutesFromTimeArrayIntoMinutesArray(timeArray);
var result = MinimiumTimeDifference(MinutesArray);
Console.WriteLine("Result:" + " " + result);
}
public static void GetMinutesFromTimeArrayIntoMinutesArray(string[] timeArray)
{
if (timeArray == null) { }
else
{
string timeArrayElement;
for (int i = 0; i < timeArray.Length; i++)
{
timeArrayElement = timeArray[i];
MinutesArray.Add(timeArrayElement.Substring(3));
}
}
}
public static string MinimiumTimeDifference(List<string> minutesArray)
{
if (minutesArray == null) { return null; }
else
{
var minutesArrayMinimum = minutesArray.Min();
MinutesArray.Remove(minutesArrayMinimum);
MinutesArray.Sort();
return MinutesArray[0] + " " + "minutes";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment