C
C#•5mo ago
bar3l

can anyone help? i need to print out the 3 most repeated numbers in the array

int[] arr = { 5, 2, 8, 4, 0, 6, 11, 8, 9, 11, 2, 11 };
int mostrepeat = -999;
int cnt = 0;
int maxcnt = 0;
for (int main = 0; main < 3; main++)
{
if (mostrepeat != -999)
{
Console.WriteLine(mostrepeat);
cnt = 0;
maxcnt = 0;
}


for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == mostrepeat)
continue;

for (int j = i; j < arr.Length; j++)
{
if (arr[j] == arr[i])
{
cnt++;
if (cnt > maxcnt)
{
maxcnt = cnt;
mostrepeat = arr[i];
}
}
}
}
}
int[] arr = { 5, 2, 8, 4, 0, 6, 11, 8, 9, 11, 2, 11 };
int mostrepeat = -999;
int cnt = 0;
int maxcnt = 0;
for (int main = 0; main < 3; main++)
{
if (mostrepeat != -999)
{
Console.WriteLine(mostrepeat);
cnt = 0;
maxcnt = 0;
}


for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == mostrepeat)
continue;

for (int j = i; j < arr.Length; j++)
{
if (arr[j] == arr[i])
{
cnt++;
if (cnt > maxcnt)
{
maxcnt = cnt;
mostrepeat = arr[i];
}
}
}
}
}
30 Replies
Pobiega
Pobiega•5mo ago
int[] arr = { 5, 2, 8, 4, 0, 6, 11, 8, 9, 11, 2, 11 };
int mostrepeat = -999;
int cnt = 0;
int maxcnt = 0;
for (int main = 0; main < 3; main++)
{
if (mostrepeat != -999)
{
Console.WriteLine(mostrepeat);
cnt = 0;
maxcnt = 0;
}


for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == mostrepeat)
continue;

for (int j = i; j < arr.Length; j++)
{
if (arr[j] == arr[i])
{
cnt++;
if (cnt > maxcnt)
{
maxcnt = cnt;
mostrepeat = arr[i];
}
}
}
}
}
int[] arr = { 5, 2, 8, 4, 0, 6, 11, 8, 9, 11, 2, 11 };
int mostrepeat = -999;
int cnt = 0;
int maxcnt = 0;
for (int main = 0; main < 3; main++)
{
if (mostrepeat != -999)
{
Console.WriteLine(mostrepeat);
cnt = 0;
maxcnt = 0;
}


for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == mostrepeat)
continue;

for (int j = i; j < arr.Length; j++)
{
if (arr[j] == arr[i])
{
cnt++;
if (cnt > maxcnt)
{
maxcnt = cnt;
mostrepeat = arr[i];
}
}
}
}
}
bar3l
bar3lOP•5mo ago
how'd u add the color
Pobiega
Pobiega•5mo ago
$code
MODiX
MODiX•5mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Pobiega
Pobiega•5mo ago
see the cs at the top?
bar3l
bar3lOP•5mo ago
oh
Pobiega
Pobiega•5mo ago
thats what sets the syntax highlighting on the same row as the backticks
bar3l
bar3lOP•5mo ago
didnt work
Pobiega
Pobiega•5mo ago
no spaces either
bar3l
bar3lOP•5mo ago
int[] arr = { 5, 2, 8, 4, 0, 6, 11, 8, 9, 11, 2, 11 };
int mostrepeat = -999;
int cnt = 0;
int maxcnt = 0;
for (int main = 0; main < 3; main++)
{
if (mostrepeat != -999)
{
Console.WriteLine(mostrepeat);
cnt = 0;
maxcnt = 0;
}


for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == mostrepeat)
continue;

for (int j = i; j < arr.Length; j++)
{
if (arr[j] == arr[i])
{
cnt++;
if (cnt > maxcnt)
{
maxcnt = cnt;
mostrepeat = arr[i];
}
}
}
}
}
int[] arr = { 5, 2, 8, 4, 0, 6, 11, 8, 9, 11, 2, 11 };
int mostrepeat = -999;
int cnt = 0;
int maxcnt = 0;
for (int main = 0; main < 3; main++)
{
if (mostrepeat != -999)
{
Console.WriteLine(mostrepeat);
cnt = 0;
maxcnt = 0;
}


for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == mostrepeat)
continue;

for (int j = i; j < arr.Length; j++)
{
if (arr[j] == arr[i])
{
cnt++;
if (cnt > maxcnt)
{
maxcnt = cnt;
mostrepeat = arr[i];
}
}
}
}
}
bar3l
bar3lOP•5mo ago
anyway that's what im getting
No description
Pobiega
Pobiega•5mo ago
well yeah, your code only stores the single most repeated number, as far as I can tell
bar3l
bar3lOP•5mo ago
can we vc ill explain it
Pobiega
Pobiega•5mo ago
#vc-2
Jester
Jester•5mo ago
i dont recommend this solution but LINQ is fun. output: [ 5, 3, 1]
(new int[] { 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 5, })
.GroupBy(i => i)
.OrderByDescending(g => g.Count())
.Select(g => g.Key)
.Take(3)
.ToArray()
(new int[] { 1, 1, 2, 3, 3, 3, 4, 5, 5, 5, 5, })
.GroupBy(i => i)
.OrderByDescending(g => g.Count())
.Select(g => g.Key)
.Take(3)
.ToArray()
Salman
Salman•5mo ago
without looking at the other's solutions. Here's mine dumb one :
c#
var freqMap = new Dictionary<int,int>();
foreach(var num in arr){
if(freqMap.ContainsKey(num)
freqMap[num]++;
else
freqMap[num] = 1;
}
var topThreeMostRepeated = freqMap.OrderByDescending(x => x.Value)
.ThenBy(x => x.Key)
.Take(3)
.Select(x => x.Key)
.ToList();
c#
var freqMap = new Dictionary<int,int>();
foreach(var num in arr){
if(freqMap.ContainsKey(num)
freqMap[num]++;
else
freqMap[num] = 1;
}
var topThreeMostRepeated = freqMap.OrderByDescending(x => x.Value)
.ThenBy(x => x.Key)
.Take(3)
.Select(x => x.Key)
.ToList();
bar3l
bar3lOP•5mo ago
@Pobiega i guess ur dictionary is the only way lol
Salman
Salman•5mo ago
:kekw:
Jester
Jester•5mo ago
skill issue you got close to mine
Salman
Salman•5mo ago
yeah yours was more concise and clever. I didn't think I could do this with grouping lol
Pobiega
Pobiega•5mo ago
well, dictionary or groupby are by far the easiest ways, yeah you absolutely can do this with arrays, but im not sure you can do it with a singular array, in a nice way...'
Salman
Salman•5mo ago
LINQ is the beauty of C#. You would rarely find feature similar to LINQ in other langs lol
Pobiega
Pobiega•5mo ago
yesn't most functional languages have something very similar. ocaml, f#, haskell, gleam, rust, elm.. hell even Java with their "we have linq at home" streams api that said, poor Alonuis is a student and his teacher has only gone through int and int[] so far :p I dont think the teacher would be very happy to see a 5-method linq chain 😄
Salman
Salman•5mo ago
:kekw: this had me dying
Pobiega
Pobiega•5mo ago
fun story, I took a university course for "intro to C#" when I already had ~ 5 years of work experience with it
Salman
Salman•5mo ago
well at least js doesn't have it (there are some third party libs but not built in) , so I can flex the c# LINQ
Pobiega
Pobiega•5mo ago
just for "free credits" so to speak lets just say that the TAs were very sceptical before I told them I worked as a C# dev 😄
bar3l
bar3lOP•5mo ago
@Pobiega i fixed it
No description
bar3l
bar3lOP•5mo ago
it was j=i idk now it works had to use a copy u cant otherwise
Jester
Jester•5mo ago
i love groupby, very confusing but powerfull
Want results from more Discord servers?
Add your server