Вариант на Linq, Extension methods и generic (можно использовать с любым типом массивов).
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace easyMatrix
{
class Program
{
static void Main(string[] args)
{
var arr = GetTestArray();
// Вывод массива
for (int i = 0; i < arr.GetLength(0); i++)
{
Console.WriteLine(string.Join(" ", arr.GetRow(i).Select(item => item.Value)));
}
Console.WriteLine();
foreach(var row in arr.GetAllRows())
{
foreach (var range in row.Split((cur, next) => cur.Value != next.Value))
{
Console.WriteLine("- [{0} {1}] {2} {3}", range.Item1.Row + 1, range.Item1.Col + 1, range.Item1.Value, range.Item2);
}
}
foreach (var col in arr.GetAllCols())
{
foreach (var range in col.Split((cur, next) => cur.Value != next.Value))
{
Console.WriteLine("| [{0} {1}] {2} {3}", range.Item1.Row + 1, range.Item1.Col + 1, range.Item1.Value, range.Item2);
}
}
foreach (var d in arr.GetAllRightDioganals())
{
foreach (var range in d.Split((cur, next) => cur.Value != next.Value))
{
Console.WriteLine("\\ [{0} {1}] {2} {3}", range.Item1.Row + 1, range.Item1.Col + 1, range.Item1.Value, range.Item2);
}
}
foreach (var d in arr.GetAllLeftDioganals())
{
foreach (var range in d.Split((cur, next) => cur.Value != next.Value))
{
Console.WriteLine("/ [{0} {1}] {2} {3}", range.Item1.Row + 1, range.Item1.Col + 1, range.Item1.Value, range.Item2);
}
}
Console.WriteLine();
var lookup = arr.GetRow(0).ToLookup(c => c.Value);
Console.WriteLine("Lookup второй строки:");
foreach (var g in lookup)
{
Console.WriteLine("'{0}' {1}", g.Key, g.Count());
}
Console.WriteLine();
var group = lookup.OrderByDescending(g => g.Count()).First();
Console.WriteLine("Максимальное количество вхождений {0} обнаружено для символа '{1}'", group.Count(), group.Key);
Console.WriteLine("Обчщее количество повторяющихся символов {0}", lookup.Where(g => g.Count() > 1).Sum(s => s.Count()));
Console.ReadKey();
}
public static Char[,] GetTestArray()
{
var lst = new string[]{
"b=b==",
"1b2ba",
"c4b++",
"5cab+"
};
Char[,] arr = new Char[lst.Count(), lst.First().Length];
for (int rowIndex = 0; rowIndex < lst.Count(); ++rowIndex)
{
var row = lst[rowIndex];
for (int colIndex = 0; colIndex < lst.First().Length; ++colIndex)
{
arr[rowIndex, colIndex] = row.ElementAt(colIndex);
}
}
return arr;
}
}
public static class IEnumerableExtension
{
public static IEnumerable<Tuple<T, int>> Split<T>(this IEnumerable<T> items, Func<T, T, bool> separator)
{
T p = items.First();
int cnt = 0;
foreach (var item in items)
{
if (separator(p, item))
{
if (cnt > 1)
{
yield return Tuple.Create(p, cnt);
}
p = item;
cnt = 1;
}
else
{
++cnt;
}
}
if (cnt > 1)
{
yield return Tuple.Create(p, cnt);
}
}
}
public static class ArrayExtension
{
public class Item<T>
{
private int _row;
public int Row { get { return _row; } }
private int _col;
public int Col { get { return _col; } }
private T _value;
public T Value { get { return _value; } }
public Item(T[,] arr, int row, int col)
{
Debug.Assert(row >= 0 && col >= 0 && row < arr.GetRowCount() && col < arr.GetColCount());
_value = arr[row, col];
_row = row;
_col = col;
}
}
public static int GetRowCount<T>(this T[,] arr)
{
return arr.GetLength(0);
}
public static int GetColCount<T>(this T[,] arr)
{
return arr.GetLength(1);
}
public static IEnumerable<Item<T>> GetCol<T>(this T[,] arr, int col)
{
for (int i = 0; i < arr.GetRowCount(); i++)
{
yield return new Item<T>(arr, i, col);
}
}
public static IEnumerable<IEnumerable<Item<T>>> GetAllCols<T>(this T[,] arr)
{
for (int i = 0; i < arr.GetColCount(); i++)
{
yield return arr.GetCol(i);
}
}
public static IEnumerable<Item<T>> GetRow<T>(this T[,] arr, int row)
{
for (int i = 0; i < arr.GetColCount(); i++)
{
yield return new Item<T>(arr, row, i);
}
}
public static IEnumerable<IEnumerable<Item<T>>> GetAllRows<T>(this T[,] arr)
{
for (int i = 0; i < arr.GetRowCount(); i++)
{
yield return arr.GetRow(i);
}
}
public static IEnumerable<Item<T>> GetRightDioganal<T>(this T[,] arr, int row, int col)
{
while (row < arr.GetRowCount() && col < arr.GetColCount())
{
yield return new Item<T>(arr, row, col);
++row;
++col;
}
}
public static IEnumerable<IEnumerable<Item<T>>> GetAllRightDioganals<T>(this T[,] arr)
{
foreach (var item in arr.GetCol(0).Reverse().Skip(1) // first col items skip last
.Concat(arr.GetRow(0).Skip(1).Reverse().Skip(1)) // first row items skip first and last
)
{
yield return arr.GetRightDioganal(item.Row, item.Col);
}
}
public static IEnumerable<Item<T>> GetLeftDioganal<T>(this T[,] arr, int row, int col)
{
while (row < arr.GetRowCount() && col >= 0)
{
yield return new Item<T>(arr, row, col);
++row;
--col;
}
}
public static IEnumerable<IEnumerable<Item<T>>> GetAllLeftDioganals<T>(this T[,] arr)
{
foreach (var item in arr.GetRow(0).Skip(1) // first row items skip first
.Concat(arr.GetCol(arr.GetColCount() - 1).Skip(1).Reverse().Skip(1)) // last col skip first and last
)
{
yield return arr.GetLeftDioganal(item.Row, item.Col);
}
}
}
}