Imports System
Imports System.Collections.Generic
Imports System.Linq
Module Program
''' <summary>
''' Finds the unique intersection of two arrays or enumerable collections.
''' </summary>
''' <typeparam name="T">The data type of the elements contained in the arrays.</typeparam>
''' <param name="first">The first input sequence.</param>
''' <param name="second">The second input sequence.</param>
''' <returns>An array containing unique elements present in both input sequences.</returns>
Public Function FindArrayIntersection(Of T)(first As IEnumerable(Of T), second As IEnumerable(Of T)) As T()
' Guard clauses for parameter validation
If first Is Nothing Then Throw New ArgumentNullException(NameOf(first))
If second Is Nothing Then Throw New ArgumentNullException(NameOf(second))
' LINQ Intersect builds a HashSet behind the scenes: O(n + m) time complexity.
' .ToArray() executes the deferred query and returns a strong array type.
Return first.Intersect(second).ToArray()
End Function
Sub Main()
' Sample input arrays
Dim arr1 As Integer() = {5, 7, 1, 1, 3, 0, 6, 2}
Dim arr2 As Integer() = {2, 8, 8, 6, 9, 3, 3, 4, 4}
' Execute intersection
Dim intersection As Integer() = FindArrayIntersection(arr1, arr2)
' Display result
Console.WriteLine($"Arr1: [{String.Join(", ", arr1)}]")
Console.WriteLine($"Arr2: [{String.Join(", ", arr2)}]")
Console.WriteLine($"Intersection: [{String.Join(", ", intersection)}]")
' Output: Intersection: [3, 6, 2]
End Sub
End Module
'
' run:
'
' Arr1: [5, 7, 1, 1, 3, 0, 6, 2]
' Arr2: [2, 8, 8, 6, 9, 3, 3, 4, 4]
' Intersection: [3, 6, 2]
'