如何在powershell哈希中查找重复值

如何在powershell哈希中查找重复值,powershell,hash,duplicates,having,Powershell,Hash,Duplicates,Having,想象一下下面的散列: $h=@{} $h.Add(1,'a') $h.Add(2,'b') $h.Add(3,'c') $h.Add(4,'d') $h.Add(5,'a') $h.Add(6,'c') 哪个查询将返回两个重复的值“a”和“c” 基本上,我在寻找与以下SQL查询等价的powershell,假设表为hc1,c2: select c1 from h group by c1 having count(*) > 1 你可以试试这个: $h.GetEnumerator() |

想象一下下面的散列:

$h=@{}
$h.Add(1,'a')
$h.Add(2,'b')
$h.Add(3,'c')
$h.Add(4,'d')
$h.Add(5,'a')
$h.Add(6,'c')
哪个查询将返回两个重复的值“a”和“c”

基本上,我在寻找与以下SQL查询等价的powershell,假设表为hc1,c2:

select c1
from h 
group by c1
having count(*) > 1 
你可以试试这个:

$h.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }

Count Name Group                                                                   
----- ---- -----                                                                   
    2 c    {System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
    2 a    {System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
如果存储结果,则可以深入组以获取重复条目的密钥名。前

$a = $h.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }

#Check the first group(the one with 'c' as value)
$a[0].Group

Name Value
---- -----
6    c    
3    c 
你可以试试这个:

$h.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }

Count Name Group                                                                   
----- ---- -----                                                                   
    2 c    {System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
    2 a    {System.Collections.DictionaryEntry, System.Collections.DictionaryEntry}
如果存储结果,则可以深入组以获取重复条目的密钥名。前

$a = $h.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }

#Check the first group(the one with 'c' as value)
$a[0].Group

Name Value
---- -----
6    c    
3    c 

您可以使用另一个哈希表:

$h=@{}
$h.Add(1,'a')
$h.Add(2,'b')
$h.Add(3,'c')
$h.Add(4,'d')
$h.Add(5,'a')
$h.Add(6,'c')

$h1=@{}
$h.GetEnumerator() | foreach { $h1[$_.Value] += @($_.name) }
$h1.GetEnumerator() | where { $_.value.count -gt 1}

Name                           Value                                                                                                  
----                           -----                                                                                                  
c                              {6, 3}                                                                                                 
a                              {5, 1}                                                                                                 

您可以使用另一个哈希表:

$h=@{}
$h.Add(1,'a')
$h.Add(2,'b')
$h.Add(3,'c')
$h.Add(4,'d')
$h.Add(5,'a')
$h.Add(6,'c')

$h1=@{}
$h.GetEnumerator() | foreach { $h1[$_.Value] += @($_.name) }
$h1.GetEnumerator() | where { $_.value.count -gt 1}

Name                           Value                                                                                                  
----                           -----                                                                                                  
c                              {6, 3}                                                                                                 
a                              {5, 1}                                                                                                 

只是一个稍微不同的问题:

如何列出PowerShell阵列的重复项

但类似于Frode F的解决方案:


只是一个稍微不同的问题:

如何列出PowerShell阵列的重复项

但类似于Frode F的解决方案:


这个解决方案非常有效。日内瓦CH有免费啤酒等着你,你在附近的时候请告诉我。如果这解决了你的问题,你可能会接受,因为问题仍然悬而未决-这个解决方案非常有效。日内瓦CH有免费啤酒等着你,你在附近的时候请告诉我。如果这解决了你的问题,你可能会接受,因为问题仍然悬而未决-同样的评论,Mjolinor,你的解决方案也很有效,只要你碰巧在我的森林之颈,你也可以免费喝啤酒。同样的评论,Mjolinor,你的解决方案也很有效,只要你碰巧在我的森林之颈,你也可以免费喝啤酒。