/// <summary> /// Gets the mode of a Color[] /// </summary> /// <param name="colors">Array of Colors</param> /// <returns>Index of mode, -1 if non found</returns> private int GetModeOfColorArray(Color[] colors) { Color[] distinctcolors = colors.Distinct().ToArray(); int[] countcolors = new int[distinctcolors.Length]; int highest = 1; int highestindex = -1; var mode = false; //count how many time distinct values are in colors for (int i = 0; i < distinctcolors.Length; i++) { foreach (Color t in colors) { if (t == distinctcolors[i]) countcolors[i]++; } } //check what the highest value is for (int i = 0; i < countcolors.Length; i++) { if (countcolors[i] > highest) { highest = countcolors[i]; highestindex = i; mode = true; } } if (mode) return Array.IndexOf(colors, distinctcolors[highestindex]); return -1; }