0

I'm having issues trying to prevent duplicate values being added to a key in powershell.

Here is my code:

foreach($row in $callLog) {
    if($agentListHashTable.$agentName -contains $row.LID) {
    } else {
        $listID = $row.LID
        $agentName = $row.Agent_Name

        if($agentListHashTable.Keys -contains $agentName) {
            $agentListHashTable.$agentName += ",$listID"
        } else {
            if($agentListHashTable.$agentName.Contains($listID)) {
                break
            }
            $agentListHashTable.Add($agentName, $listID)
        }
    }
}

This adds the required keys and their values. However, in each of these keys there are multiple values that the same. An example of what the hashtable looks like is

Key: Jim Smith
Value: 1,1,1,1,1,2,2,2,2,2,22,22,44,1,1,1,1,1

However I would just like it to be:
Key: Jim Smith
Value: 1,2,22,44

I've tried doing it in the loop with something like: if($agentListHashTable.$agentName.ContainsValue($listID)

But it doesn't work and I've looked online for how to do it outside the loop by iterating through each key and removing the duplicate values but I can't find anything.

Thomton
  • 102
  • 13

2 Answers2

4

You did gave an example of the input data, but I presume it looks like:

$callLog =
    [PSCustomObject]@{ Agent_Name = 'Bob'; LID = 1 },
    [PSCustomObject]@{ Agent_Name = 'Bob'; LID = 2 },
    [PSCustomObject]@{ Agent_Name = 'Jim'; LID = 1 },
    [PSCustomObject]@{ Agent_Name = 'Jim'; LID = 1 }

Aside from the fact that using the increase assignment operator (+=) to create a collection is ineffective, I would use a HashSet<T> Class for this as it automatically handles duplicates:

$agentListHashTable = @{}
foreach($row in $callLog) {
    if (!$agentListHashTable.ContainsKey($row.Agent_Name)) {
        $agentListHashTable[$row.Agent_Name] = 
            [System.Collections.Generic.HashSet[int]]::new()
    }
    $Null = $agentListHashTable[$row.Agent_Name].Add($row.LID)
}
$agentListHashTable

Name           Value
----           -----
Jim            {1}
Bob            {1, 2}
iRon
  • 20,463
  • 10
  • 53
  • 79
  • 1
    Thanks for you response, and both of your responses helped me get the answer. There were no duplicate keys, which you inferred from my question (it was badly formatted/worded to be fair to you) but I don't think it matters in the outcome. Implementing it worked great thanks! – Thomton Jul 17 '23 at 10:50
1

You're not really collecting the individual values to a list - you're concatenating them to the same string, which is why Contains doesn't work.

Do the following instead:

foreach($row in $callLog) {
    $listID = $row.LID
    $agentName = $row.Agent_Name

    if (-not $agentListHashTable.ContainsKey($agentName)){
        # no entry with the expect key yet - let's create a list!
        $agentListHashTable[$agentName] = [System.Collections.Generic.List[psobject]]::new()
    }

    if ($listID -notin $agentListHashTable[$agentName]) {
        # entry with the given agent name key exists, but the ID is not in there yet
        $agentListHashTable[$agentName].Add($listID)
    }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206