How to get unique values from two arrays in VB.NET

1 Answer

0 votes
Imports System
Imports System.Linq

Public Class Program
	Public Shared Sub Main()
        Dim arr1 As Integer() = {1, 3, 6, 8, 12, 90}
        Dim arr2 As Integer() = {2, 3, 5, 6, 7, 8, 96}
        
		Dim result = get_unique_values(arr1, arr2)
		
        Console.WriteLine(String.Join(", ", result))
    End Sub

    Private Shared Function get_unique_values(ByVal arr1 As Integer(), ByVal arr2 As Integer()) As Integer()
        Dim diff1 = arr1.Except(arr2)
        Dim diff2 = arr2.Except(arr1)
		
        Dim result = diff1.Concat(diff2).OrderBy(Function(n) n).ToArray()
			
        Return result
    End Function
End Class


' run:
'
' 1, 2, 5, 7, 12, 90, 96
'

 



answered Feb 16, 2025 by avibootz

Related questions

...