简体   繁体   English

如何替换行二维数组中的重复项

[英]how to replace duplicates in row 2d array

I have an 2d array with chars 6x6 and looks something like this and Im trying to create an method witch needs to replace duplicates in row with '@' character我有一个 6x6 字符的二维数组,看起来像这样,我试图创建一个方法,女巫需要用“@”字符替换行中的重复项

 a b a a
 a a b c
 a a a b
 a a a a

and after replacing with method should look like this用方法替换后应该是这样的

a b @ @
a @ b c
a @ @ b
a @ @ @

I have tried this method but no results "I need to make this work without libraries"我已经尝试过这种方法但没有结果“我需要在没有库的情况下完成这项工作”

 public  void RemoveDuplicates(char[,] array)
    {
        char symbol = '@';

        for (int i = 0; i < array.GetLength(0); i++)
        {
            for (int j = 0; j < array.GetLength(1); j++)
            {
                int temp = array[i, j];
                int next = ++temp;

                if(temp == next)
                {
                    next = symbol;
                }

            }
        }
    }

You can use Hashset (or list also) collection (docs: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/collections ) to store your duplicates as follows:您可以使用 Hashset(或也列出)集合(文档: https ://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/collections)来存储您的重复项,如下所示:

    var hash = new HashSet<char>(); 

    for (int i = 0; i < array.GetLength(0); i++)
    {
        for (int j = 0; j < array.GetLength(1); j++)
        {
             if(!hash.Add(array[i,j])) array[i,j] = '@';
        }
        hash.Clear();
    }

Or list as follows:或列举如下:

    var list = new List<char>();
    
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    if(!list.Contains(array[i,j])) list.Add(array[i,j]);
                    else array[i,j] = '@';
                }
                list.Clear();
            }

Probably not the fastest solution but pretty simple to understand可能不是最快的解决方案,但很容易理解

Considering this input考虑到这个输入

var data = new char[,]
        {
            { 'a', 'b', 'a', 'a' },
            { 'a', 'a', 'b', 'c' },
            { 'a', 'a', 'a', 'b' },
            { 'a', 'a', 'a', 'a' },
        };

With this code it will take each row, one by one, then replace duplicated values使用此代码,它将逐行获取每一行,然后替换重复的值

        //for each row
        for (int i = 0; i < data.GetLength(0); i++)
        {
            //for each cell
            for (int j = 0; j < data.GetLength(1); j++)
            {
                //for each cell compare all next cells
                for (int k = data.GetLength(1) - 1; k > j; k--)
                {
                    if (data[i, j] == data[i, k])
                    {
                        data[i, k] = '@';
                    }
                }
            }
        }

Result:结果:

a b @ @
a @ b c
a @ @ b
a @ @ @

PS: You can improve a bit the code by iterating "data.GetLength(1) -1" as the last cell cant have a duplicate. PS:您可以通过迭代“data.GetLength(1) -1”来改进代码,因为最后一个单元格不能有重复项。

And skip the loop if the value of the cell is '@', in the current code you will replace some '@' by another '@' :D如果单元格的值为“@”,则跳过循环,在当前代码中,您将用另一个“@”替换一些“@”:D

The goal should be to keep track of all previously seen characters.目标应该是跟踪所有以前看到的角色。 So you need some kind of 'set' data structure for this.因此,您需要某种“集合”数据结构。 The easy way would be to use an HashSet .简单的方法是使用HashSet This has an add method that adds the value to the set, and return false if the value was already in the set.这有一个 add 方法,可以将值添加到集合中,如果值已经在集合中,则返回 false。

var seenCharacters = new HashSet<char>();
for (int j = 0; j < array.GetLength(1); j++)
{
    if(!seenCharacters.Add(array[i, j])){
         array[i, j] = symbol;
    }
}

HashSet is part of the framework, so would not normally be considered a "library". HashSet 是框架的一部分,因此通常不会被视为“库”。

If that is now allowed you could provide similar functionality with an plain array.如果现在允许这样做,您可以使用普通数组提供类似的功能。

var seenCharacters = new bool[(int)char.MaxValue];
for (int j = 0; j < array.GetLength(1); j++)
{
    var c = (int)array[i, j];
    if(seenCharacters[c]){
         array[i, j] = symbol;
    }
    seenCharacters[c] = true;
}

But I would probably not recommend such a solution since it uses much more memory compared to the hashSet.但我可能不会推荐这样的解决方案,因为与 hashSet 相比,它使用了更多的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM