# Combining Arrrays and removing dups

**URL:** <https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767>\
**Category:** PowerShell Help\
**Created:** [December 7, 2018, 8:38am UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767 "2018-12-07T08:38:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kreston-yates](https://avatars.discourse-cdn.com/v4/letter/k/9fc348/32.png) [@kreston-yates](https://forums.powershell.org/u/kreston-yates)\
**Post date:** [December 7, 2018, 8:38am UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/1 "2018-12-07T08:38:40Z")

</div>

Morning all, I’m just looking for an explanation here. It involves how to properly join arrays. If I do

```
$array1 = 1,2
$arra2 = 2,3,4
$array3 = @($array1, $array2) 
$array3 | select -unique
```

Then I get a result of 1,2,2,3,4

But if I do

```
$array1 = 1,2
$arra2 = 2,3,4
$array3 = $array1+$array2 
$array3 | select -unique
```

I’ll get what I want as 1,2,3,4

I know its my lack of understanding, but was hoping for a quick education on this.

&nbsp;

Appreciate it.

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![palhan](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@palhan](https://forums.powershell.org/u/palhan)\
**Post date:** [December 7, 2018, 8:59am UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/2 "2018-12-07T08:59:00Z")

</div>

In the first one, you create an array that contains two arrays… A jagged array.

$array3[0] is array1 and $array3[1] is array2.  
Length of array3 is 2.  
First element of array1 is $array3[0][0]

In the second, you create a flat array, with the length of 5.

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [December 7, 2018, 9:21am UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/3 "2018-12-07T09:21:55Z")

</div>

phansen nailed it, indeed!

For reference, when doing this sort of thing with large collections that you need to get unique values out of, you can often simply combine the arrays and then cast to a HashSet\<T\> – this is a generic object type that can hold any one specific defined type of objects, and this cast will automatically remove duplicates as a Hashset can only contain unique values.

> <https://gist.github.com/vexx32/aaddacd11dfe26871ffcae13cc86ce09>

---

<div class="post-metadata">

**Author:** ![kreston-yates](https://avatars.discourse-cdn.com/v4/letter/k/9fc348/32.png) [@kreston-yates](https://forums.powershell.org/u/kreston-yates)\
**Post date:** [December 7, 2018, 11:22am UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/4 "2018-12-07T11:22:18Z")

</div>

Thanks all I appreciate it. If I wanted to take this a bit further how would I accomplish the below?

Combine two arrays, do a compare on the arrays for matches and then remove an entry?

So using the previous example

```
$array1 = 1,2
$arra2 = 2,3,4
$array3 = $array1+$array2 | select -unique
```

Will return the 1,2,3,4

Then compare to another list of values

```
$masterlist = "1","4"

$matches = compare-object -referenceobject $array3 -differenceobject $masterlist -excludedifferent -includeequal | select -expand inputobject
```

I’ll now see a $matches = 1,4

but If I want to remove, say “4”, how would I do this?

If I use the Hashset, it lists all the items as one argument.

If I try

```
$master.Remove("4")
```

I get an error since the array is fixed and cannot be changed.

If I change the $master to a arraylist and try the .remove I still get an error?

&nbsp;

I know i’m a bit out of my element here, but would appreciate any help.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [December 7, 2018, 12:04pm UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/5 "2018-12-07T12:04:29Z")

</div>

how did you change master to arraylist ?

```
$r = New-Object -TypeName System.Collections.ArrayList
$r.Add(1)
$r.Add(2)
$r
$r.Remove(1)
$r
```

---

<div class="post-metadata">

**Author:** ![kreston-yates](https://avatars.discourse-cdn.com/v4/letter/k/9fc348/32.png) [@kreston-yates](https://forums.powershell.org/u/kreston-yates)\
**Post date:** [December 7, 2018, 4:15pm UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/6 "2018-12-07T16:15:14Z")

</div>

I was trying with the $matches array, but wasn’t successful.

```
[System.Collections.ArrayList]@($Master = $Master)
$Master.remove(4)
```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [December 8, 2018, 8:37am UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/7 "2018-12-08T08:37:15Z")

</div>

If $Master is a defined array, this is how you do it.  
[System.Collections.ArrayList]@($Master)

---

<div class="post-metadata">

**Author:** ![kreston-yates](https://avatars.discourse-cdn.com/v4/letter/k/9fc348/32.png) [@kreston-yates](https://forums.powershell.org/u/kreston-yates)\
**Post date:** [December 10, 2018, 8:08am UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/8 "2018-12-10T08:08:34Z")

</div>

Appreciate the help!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:34pm UTC](https://forums.powershell.org/t/combining-arrrays-and-removing-dups/11767/9 "2024-05-16T20:34:06Z")

</div>

Closed on May 16, 2024, 8:34pm UTC


