arrays - PowerShell:两个以上数组的交集

标签 arrays powershell set-intersection

使用 PowerShell,我有 14 个字符串数组。一些数组是空的。我将如何获得这些数组(不包括空数组)的交集(所有数组中存在的所有元素)?我试图避免一次比较两个数组。

一些数组是空的,所以我不想在我的比较中包括那些。关于我将如何处理这个问题的任何想法?谢谢。

$a = @('hjiejnfnfsd','test','huiwe','test2')
$b = @('test','jnfijweofnew','test2')
$c = @('njwifqbfiwej','test','jnfijweofnew','test2')
$d = @('bhfeukefwgu','test','dasdwdv','test2','hfuweihfei')
$e = @('test','ddwadfedgnh','test2')
$f = @('test','test2')
$g = @('test','bjiewbnefw','test2')
$h = @('uie287278hfjf','test','huiwhiwe','test2')
$i = @()
$j = @()
$k = @('jireohngi','test','gu7y8732hbj','test2')
$l = @()
$m = @('test','test2')
$n = @('test','test2')

我尝试解决这个问题(虽然它不检查空数组):

$overlap = Compare-Object $a $b -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $c -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $d -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $e -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $f -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $g -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $h -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $i -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $j -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $k -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $l -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $m -PassThru -IncludeEqual -ExcludeDifferent
$overlap = Compare-Object $overlap $n -PassThru -IncludeEqual -ExcludeDifferent

我想要的结果是 test 和 test2 出现在 $overlap 中。此解决方案不起作用,因为它不检查正在比较的数组是否为空。

最佳答案

注意:以下假设没有单个数组多次包含相同的字符串(需要做更多的工作来解决)。

$a = @('hjiejnfnfsd','test','huiwe','test2')
$b = @('test','jnfijweofnew','test2')
$c = @('njwifqbfiwej','test','jnfijweofnew','test2')
$d = @('bhfeukefwgu','test','dasdwdv','test2','hfuweihfei')
$e = @('test','ddwadfedgnh','test2')
$f = @('test','test2')
$g = @('test','bjiewbnefw','test2')
$h = @('uie287278hfjf','test','huiwhiwe','test2')
$i = @()
$j = @()
$k = @('jireohngi','test','gu7y8732hbj','test2')
$l = @()
$m = @('test','test2')
$n = @('test','test2')

$allArrays = $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n

# Initialize a hashtable in which we'll keep
# track of unique strings and how often they occur.
$ht = @{}

# Loop over all arrays.
$nonEmptyArrayCount = 0
foreach ($arr in $allArrays) {
  # Loop over each non-empty array's elements.
  if ($arr.Count -gt 0) {
    ++$nonEmptyArrayCount 
    foreach ($el in $arr) {
      # Add each string and increment its occurrence count.
      $ht[$el] += 1
    }
  }
}

# Output all strings that occurred in every non-empty array
$ht.GetEnumerator() |
  Where-Object Value -eq $nonEmptyArrayCount |
  ForEach-Object Key

以上输出那些存在于所有非空输入数组中的字符串:

test2
test

关于arrays - PowerShell:两个以上数组的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72608938/

相关文章:

java - 为什么 Java 不允许扩展数组类型

windows - 如何在 PowerShell 中将位置与主文件夹进行比较

c++ - 使用带有 set_intersection 的 map

algorithm - 非不相交集之间的高效交集操作

javascript - 如何提高 javascript 数组操作中嵌套循环的性能?

javascript - 将两个数组合并为所有可能组合的数组的算法

Javascript 二维数组

node.js - PowerShell 的 async.parallel?

powershell - 使用 PowerShell 替换文件中的字符串

c++ - 调试断言失败 - 数组迭代器不兼容