How to remove duplicate maximum and minimum from unsorted array in VB.NET

1 Answer

0 votes
Imports System
Imports System.Linq

Public Class Example
    Public Shared Sub Main(ByVal args As String())
        Dim arr = {3, 5, 1, 6, 1, 1, 9, 8, 9, 9, 9, 7, 4}
        Dim max = arr.Max()
        Dim min = arr.Min()
        Dim onemin = 0 ' for fist time only: i > min (1) = no onemin++ == 0 = yes, next time onemin = 1, then 2...
        Dim onemax = 0 ' for fist time only: i > max (9) = no onemax++ == 0 = yes, next time onemax = 1, then 2...
		
        Dim result = arr.Where(Function(i) (i > min OrElse Math.Min(System.Threading.Interlocked.Increment(onemin), onemin - 1) = 0) AndAlso (i < max OrElse Math.Min(System.Threading.Interlocked.Increment(onemax), onemax - 1) = 0))
			
        Console.WriteLine(String.Join(" ", result))
    End Sub
End Class
	
	
	
	

' run:
'
' 3 5 1 6 9 8 7 4
'

 



answered Dec 13, 2022 by avibootz
...