Using Powershell, how can i count the occurrence of each element in an array?
问题 If I have an array: 1 1 1 2 2 3 4 4 4 4 5 5 How can I use Powershell to tell me how many of each element there are in that array? To be a little more clear, I should have a separate count for each array element: Element:Count 1:3 2:2 3:1 4:4 5:2 回答1: You can use the Group-Object cmdlet: PS> 1,1,1,2,2,3,4,4,4,4,5,5 | group Count Name Group ----- ---- ----- 3 1 {1, 1, 1} 2 2 {2, 2} 1 3 {3} 4 4 {4, 4, 4, 4} 2 5 {5, 5} If you want a hashtable for the items and their counts you just need a little