简体   繁体   English

查找多个数组中所有匹配元素的索引

[英]Find all the index of matching elements in the multiple Array

Suppose I have two arrays as follows 假设我有两个数组如下

int[] first = { 1, 2, 3, 4, 5, 6, 12, 13, 14 };
int[] second = { 12, 13, 14, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };

I want the result as follows: 我想要的结果如下:

matching index from the first = 6,7,8 
matching index from second = 0,1,2 

Condition: I cannot sort the array to find the index and there can be any number of the array. 条件:我无法对数组进行排序以找到索引,并且可以有任意数量的数组。

I am looking for some efficient solution and I will be glad for the help. 我正在寻找一些有效的解决方案,我将很高兴为您提供帮助。 Thanks in advance. 提前致谢。

Below is the code I did for the two arrays: 下面是我为两个数组编写的代码:

class Program
{
    static void Main(string[] args)
    {
        int[] first = { 1, 2, 3, 4, 5, 6, 12, 13, 14 };
        int[] second = { 12, 13, 14, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
        IndexArray sameIndexArray = CompareArray(first, second);
        Console.WriteLine("FOLLOWING ARE THE INDEX WITH SAME VALUE FOR FIRST ARRAY");
        foreach (var index in sameIndexArray.FirstArray)
        {
            Console.WriteLine(index);
        }
        Console.WriteLine("FOLLOWING ARE THE INDEX WITH SAME VALUE FOR SECOND ARRAY");
        foreach (var index in sameIndexArray.SecondArray)
        {
            Console.WriteLine(index);
        }

        Console.ReadKey();
    }

    private static IndexArray CompareArray(int[] firstArray, int[] secondArray)
    {
        IndexArray arrayIndex = new IndexArray();
        arrayIndex.FirstArray = new List<int>();
        arrayIndex.SecondArray = new List<int>();
        for (int i = 0; i < firstArray.Length; i++)
        {
            for (int j = 0; j < secondArray.Length; j++)
            {
                if (firstArray[i] == secondArray[j])
                {
                    arrayIndex.FirstArray.Add(i);
                    arrayIndex.SecondArray.Add(j);
                }
            }
        }

        return arrayIndex;
    }
}

public class IndexArray
{
    public List<int> FirstArray { get; set; }
    public List<int> SecondArray { get; set; }
}

Your solution is O(N^2). 您的解决方案是O(N ^ 2)。 An O(N) or O(N log N) solution should be possible: O(N)或O(N log N)解决方案应该是可能的:

  • Create a HashSet for each of the sets 为每个集合创建一个HashSet
  • iterate over the first set, filtering by hashset2.Contains and print the indexes 遍历第一个集合,通过hashset2过滤。包含并打印索引
  • do the same vice versa 反之亦然

Something like this: 像这样:

private static IndexArray CompareArray(int[] firstArray, int[] secondArray)
{
    IndexArray arrayIndex = new IndexArray();
    var hashset2 = new HashSet<int>(secondArray);
    for (int i = 0; i < firstArray.Length; i++)
    {
        if (hashset2.Contains(firstArray[i]))
            arrayIndex.FirstArray.Add(i);
    }
    var hashset1 = new HashSet<int>(firstArray);
    for (int i = 0; i < secondArray.Length; i++)
    {
        if (hashset1.Contains(secondArray[i]))
            arrayIndex.SecondArray.Add(i);
    }

    return arrayIndex;
}

If this is working code it might be a better fit on code review. 如果这是有效的代码,则可能更适合代码审查。

I would drop the 我会放弃

arrayIndex.FirstArray = new List<int>();
arrayIndex.SecondArray = new List<int>();

Add

public List<int> FirstArray  { get; } = new List<int>();
public List<int> SecondArray { get; } = new List<int>();

Arraylookup is fast but I would add Arraylookup速度很快,但我会添加

int first = firstArray[i];

And then use that. 然后使用它。

WritelLine will write a line. WritelLine将写一行。

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

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