Help cleaning an array of ‘duplicates’

Welcome Forums General PowerShell Q&A Help cleaning an array of ‘duplicates’

Viewing 3 reply threads
  • Author
    Posts
    • #263373
      Participant
      Topics: 22
      Replies: 31
      Points: 14
      Rank: Member

      I have a script that generates an array that has output as shown below:

      [test group2] contains [test group1]
      [test group3] contains [test group2]
      [test group1] contains [test group2]
      [test group2] contains [test group3]

       

      The lines such as [test group2] contains [test group1] and [test group1] contains [test group2] are essentially duplicates, just in a different order. I really only need one or the other, not important which I keep.

      How could I go about clearing out any duplicates from the array?

    • #263385
      Senior Moderator
      Topics: 10
      Replies: 184
      Points: 980
      Helping Hand
      Rank: Major Contributor

      Hmmm… if [test group1] = [test group2] then
      [test group1] contains [test group2] = [test group2] contains [test group1]

      but if [test group1] != [test group2] then
      [test group1] contains [test group2] != [test group2] contains [test group1]

      I’m not really following the logic here…

    • #263394
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      So the same would be true for [test group2] contains [test group3] and [test group3] contains [test group2], correct? If so you could just use a switch utilizing regex with a list and only add to the list if both don’t already exist in a line. It’s a lot easier to show than express with words so please see the example.

      To simulate a Get-Content $somefile we’ll use a here-string split at new lines. Then we’ll prepare an empty list and finally process each line through the switch.

      And what ends up in $result are the first two that weren’t duplicated.

      The switch statement can also read directly from files. See the following expanded example.

    • #263400
      Participant
      Topics: 22
      Replies: 31
      Points: 14
      Rank: Member

      This works perfectly!

       

      Thank you!

Viewing 3 reply threads
  • You must be logged in to reply to this topic.