Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

How to find the intersection of two arrays (values that exist in both arrays) in VB.NET

1 Answer

0 votes
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]
'

					

 



answered Sep 28 by avibootz
...