如何在2D数组中水平和垂直检查重复值?

时间:2016-04-26 16:51:53

标签: c# arrays multidimensional-array

我们说我有一个这样的数组:

9 1 2 0 
1 5 2 5 
7 1 6 3 
4 3 2 7 

我希望能够创建一个垂直和水平遍历数组的循环,以查看是否有任何重复。
例如,它将首先检查9 1 7 4以查看是否存在重复项。然后是1 5 1 3,依此类推 在那之后,它会做9 1 2 0(这会告诉我那里有重复),然后是1 5 2 5 7等等。 我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

虽然可能,嵌套循环解决方案可能不是解决它的更直接的方法。在这种情况下,使用LINQ必须更容易:

var matrix = new int[4, 4]
{
    { 9, 1, 2, 0 },
    { 1, 5, 2, 5 },
    { 7, 1, 6, 3 },
    { 4, 3, 2, 7 }
};

for (int i = 0; i < 4; i++)
{
    var row = Enumerable.Range(0, 4).Select(x => matrix[i, x]);
    if (row.Distinct().Count() != 4)
        Console.WriteLine("Duplicated value in row {0} : {1}", 
            i + 1, string.Join(", ", row));
}

for (int i = 0; i < 4; i++)
{
    var column = Enumerable.Range(0, 4).Select(x => matrix[x, i]);
    if (column.Distinct().Count() != 4)
        Console.WriteLine("Duplicated value in column {0} : {1}", 
            i + 1, string.Join(", ", column));
}

输出:

Duplicated value in row 2 : 1, 5, 2, 5
Duplicated value in column 2 : 1, 5, 1, 3
Duplicated value in column 3 : 2, 2, 6, 2

答案 1 :(得分:1)

我已经使用嵌套的For循环获得了一个有效的解决方案。当用于检查(水平或垂直)的索引为零时,这将向控制台写入“Nothing”以避免ArrayIndexOutOfBoundsException。它写道“重复号码”然后是复制到控制台的数字。我在下面发布了我的完整工作示例以及控制台的输出:

横向:

int[,] array = new int[5, 4] { { 1, 2, 3, 4 }, { 5, 5, 5, 5 }, { 9, 5, 11, 12 }, { 13, 14, 15, 16 }, { 17, 18, 19, 20 } } ;

for (int i = 0; i < array.GetLength(0); i++)
{
    for (int j = 0; j < array.GetLength(1); j++)
    {
        if (j == 0)
        {
            Console.WriteLine("Nothing");
        }
        else if (array[i, j] == array[i, j - 1])
        {
            Console.WriteLine("Duplicate No." + array[i, j].ToString());
        }
    }
}

垂直:

for (int i = 0; i < array.GetLength(1); i++)
{
    for (int j = 0; j < array.GetLength(0); j++)
    {
        if (j == 0)
        {
            Console.WriteLine("Nothing");
        }
        else if (array[j, i] == array[j - 1, i])
        {
            Console.WriteLine("Duplicate No." + array[i, j].ToString());
        }

    }
}

水平和垂直输出:

Nothing
Nothing
Duplicate No. 5
Duplicate No. 5
Duplicate No. 5
Nothing
Nothing
Nothing
Nothing
Nothing
Duplicate No. 5
Nothing
Nothing

答案 2 :(得分:0)

我创建了一个允许您枚举数组的类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace ConsoleApplication88
{
    class Program
    {
        static void Main(string[] args)
        {
            EnumerateArray eArray = new EnumerateArray() {
                new List<int>() {1,2,3,4},
                new List<int>() {5,6,7,8},
                new List<int>() {9,10,11,12},
                new List<int>() {13,14,15,16},
                new List<int>() {17,18,19,20}
            };
            foreach (List<int> x in eArray)
            {
                Console.WriteLine(string.Join(",", x.Select(y => y.ToString()).ToArray()));
            }
            Console.ReadLine();

        }
    }

    public class EnumerateArray : IEnumerator, IEnumerable
    {
        public List<List<int>> myArray { get; set;}
        int row = 0;
        int col = 0;
        int numCols = 0;
        int numRows = 0;
        public int Count { get; set; }
        public int[] current = null;
        Boolean finishedCol = false;

        public EnumerateArray()
        {
            myArray = new List<List<int>>();
        }
        public EnumerateArray(List<List<int>> array)
        {
            myArray = array;
            Reset();
            numRows = array.Count;
            numCols = array[0].Count;
            Count = numCols + numRows;
        }
        public void Add(List<int> array)
        {
            myArray.Add(array);
            numRows = myArray.Count;
            numCols = array.Count;
            Count = numCols + numRows;
        }
        public void Add(List<List<int>> array)
        {
            myArray = array;
            Reset();
            numRows = array.Count;
            numCols = array[0].Count;
            Count = numCols + numRows;
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)GetEnumerator();
        }

        public EnumerateArray GetEnumerator()
        {
            return new EnumerateArray(myArray);
        }

        public bool MoveNext()
        {
            Boolean done = true;
            if (finishedCol == false)
            {
                if (col < numCols - 1)
                {
                    col++;
                }
                else
                {
                    finishedCol = true;
                    row = 0;
                }
            }
            else
            {
                if (row < numRows - 1)
                {
                    row++;
                }
                else
                {
                    done = false;
                }
            }

            return done;
        }

        public void Reset()
        {
            row = -1;
            col = -1;
            finishedCol = false;
            Count = numCols + numRows;
        }
        object IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }

        public List<int> Current
        {
            get
            {
                try
                {
                    List<int> _array = new List<int>();
                    if (finishedCol == false)
                    {

                        for (int _row = 0; _row < numRows; _row++)
                        {
                            _array.Add(myArray[_row][ col]);
                        }
                    }
                    else
                    {
                        _array.AddRange(myArray[row]);
                    }
                    return _array;
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }

}