# Mode of list of numbers - VB [closed]

I have an array of numbers created by the user, the array is sorted and I can find the Average, Median, Max, Min, Lowest -> Highest, Highest -> Lowest. Now I just need to find the mode. Here is the code I made that works if there is only 1 Mode, I need to make it work to find 3 modes .. I.e. if the array was [1,1,1,2,2,2,3,3,3,5,6] then the mode would be 1 , 2 , 3

    Dim countermode As Integer
Dim w, s, r, i, p, tempmode As Integer
p = 1
Sort(marks)

For w = (x - 1) To 0 Step -1

p += 1

For s = (x - p) To 0 Step -1
r = marks(w)
i = marks(s)

If i = r Then
countermode += 1
tempmode = marks(w)
End If
Next

Next

lblmode.Text = tempmode

-

## closed as off-topic by RubberDuck, Simon Forsberg♦, Kid Diamond, ChrisWue, Jamal♦Sep 20 '14 at 22:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – RubberDuck, Simon Forsberg, Kid Diamond, ChrisWue, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

what is mode? it is related to the sorting logic. – paritosh Dec 21 '12 at 16:09
@paritosh - Mode is the subset of numbers which appears the most frequently within a set. – Bobson Dec 21 '12 at 16:20
A wonderful write-up on just this functionality in Linq can be found here: LinqMode – dgp Feb 6 '13 at 15:28

In C#:

var list = arr.GroupBy(x => x)
.Select(x => new { Count = x.Count(), Value = x.Key })
.GroupBy(x => x.Count)
.OrderByDescending(x => x.Key)
.First()
.ToList();


Automatically converted to VB.NET:

Dim list = arr.GroupBy(Function(x) x) _
.[Select](Function(x) New With { _
Key .Count = x.Count(), _
Key .Value = x.Key _
}) _
.GroupBy(Function(x) x.Count) _
.OrderByDescending(Function(x) x.Key) _
.First() _
.ToList()


You might want to make it more idiomatic.

-
Fascinating, +1 for the C# code, -1 for the VB code, that stuff is just plain horrible to look at and should not be. – konijn Dec 21 '12 at 16:09
As I said, it was an automatic conversion. I didn't learn to work with LINQ until after I switched to c#, so I couldn't tell you what it should look like. – Bobson Dec 21 '12 at 16:16
Edited to make it look cleaner, at the very least. – Bobson Dec 21 '12 at 16:18
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.24149110913276672, "perplexity": 2475.358158516948}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2016-26/segments/1466783395548.53/warc/CC-MAIN-20160624154955-00012-ip-10-164-35-72.ec2.internal.warc.gz"}