Reputation: 11
Let's say I have an array like this :
9 1 2 0
1 5 2 5
7 1 6 3
4 3 2 7
I want to be able to create a loop that goes through the array vertically and horizontally to see if there's any duplicates.
For example, it will first check 9 1 7 4 to see if there's a dups. Then 1 5 1 3 and so on.
After that, it'll do 9 1 2 0 (which will tell me there's a dup), then 1 5 2 5 7, etc etc.
How can I do that?
Upvotes: 1
Views: 2546
Reputation: 34421
I create a class that allow you to enumerate through the array
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();
}
}
}
}
}
Upvotes: 0
Reputation: 179
I have gotten a working solution using nested For Loops. This will write "Nothing" to the console when the index used to check(horizontal or vertical) is zero to avoid an ArrayIndexOutOfBoundsException. It writes "Duplicate No." and then the number that was duplicate to the console. I have posted my full working example below along with the output from console:
For horizontal:
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 vertical:
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());
}
}
}
Output from both horizontal and vertical:
Nothing
Nothing
Duplicate No. 5
Duplicate No. 5
Duplicate No. 5
Nothing
Nothing
Nothing
Nothing
Nothing
Duplicate No. 5
Nothing
Nothing
Upvotes: 1
Reputation: 14477
While possible, a nested loops solution may not be the more straightforward way of solving it. In this case, using LINQ is must easier :
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));
}
Output :
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
Upvotes: 2