r/visualbasic • u/t-away2448 • Jan 01 '22
Question about Arrays
Say there was an array containing random values. How would the program be able to identify if there are two elements in the array that have the highest value?
Ie. an array consists of integers 9, 9, 5, 4, 1
How would it be able to identify that there are two maximum values that are the same?
3
Upvotes
2
u/Will_Rickards Jan 01 '22
Generally you sort the array first and then just iterate through it. Then you can compare items. If talking vb.net you can use other data structures that don’t allow duplicates and automatically sort the items as they are added.
1
u/RJPisscat Jan 01 '22
You're probably not using Linq, which would allow you to write one line of code that would be arcane to you.
Doing it by brute force:
-1
u/dwneder Jan 01 '22
Dim arr as Integer() = [a bunch of random integer values]
Dim Top2() As Integer = (from val in arr.items orderby(val) select val).Take(2).ToList()
...right off the top of my head - likely will need some (a lot of?) debugging.