Program in VB.net to find the numbers larger than them in the collection.

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 240
Let's say we have a collection of numbers like { 16,17,4,3,5,2 }. Now the objective is to find those numbers which are greater than the rest in the collection while compare with their right elements.
Indicates that 16 compared to 17 is less and cannot be consider. While 17 compared to 4,3 5 and 2 is always greater and hence will be consider. Likewise, 4 though greater than 3 abut less than 5 will be discarded. But 5 compared to 2 is greater. And since 2 is the rightmost element will always be consider.
The below program will do so

Imports System.Collections.Generic
Imports System.Linq

Namespace ConsoleApplication2
Class Program
Private Shared Sub Main(args As String())

Dim intCollection = New List(Of Integer)() From { _
16, _
17, _
4, _
3, _
5, _
2 _
}
Dim discardedElements = New List(Of Integer)()

For i As Integer = 0 To intCollection.Count - 1
For j As Integer = i + 1 To intCollection.Count - 1
If intCollection(i) < intCollection(j) Then
discardedElements.Add(intCollection(i))
End If
Next
Next
Console.WriteLine("Successful elements are")
intCollection.Except(discardedElements).ToList().ForEach(Function(i) Console.WriteLine("{0}", i))
Console.ReadKey()
End Sub
End Class
End Namespace


Result
-------
Successful elements are
17
5
2

Comments or Responses

Login to post response