-1

I wont to go though a array and remove any object/value which is duplicated in the array.

I try this code the convert the string into array

$a = "a,b,a,c,d,a,g,G,h,A,,,,"
$option = [System.StringSplitOptions]::RemoveEmptyEntries
$a= $a.Split(",", $option);
$a= $TextInfo.ToTitleCase($a.Trim())

Then I use following to remove any values which is duplicated

$a = $a | Select-Object -Unique

Which is hit and miss on some list, its working for some list but then for some it doesn't.

Would like to have a array with no duplicate values

0

2 Answers 2

1

You could do this:

$a = "a,b,a,c,d,a,g,G,h,A,,,," -split ',' | 
    Where-Object { $_ -match '\S' } |
    ForEach-Object {(Get-Culture).TextInfo.ToTitleCase($_.Trim())} | 
    Select-Object -Unique

$a

Output:

A
B
C
D
G
H
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like if I use this approached it works stackoverflow.com/questions/16153609/…
0

You could convert your split array to uppercase with ToUpper() before getting unique values:

❯ $a = "a,b,a,c,d,a,g,G,h,A,,,,"
❯ $option = [System.StringSplitOptions]::RemoveEmptyEntries
❯ $a.Split(",", $option).ToUpper() | Select-Object -Unique
A
B
C
D
G
H

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.