C
C#•4mo ago
Alonuis

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•4mo 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];
}
}
}
}
}
Alonuis
AlonuisOP•4mo ago
how'd u add the color
Pobiega
Pobiega•4mo ago
$code
MODiX
MODiX•4mo 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•4mo ago
see the cs at the top?
Alonuis
AlonuisOP•4mo ago
oh
Pobiega
Pobiega•4mo ago
thats what sets the syntax highlighting on the same row as the backticks
Alonuis
AlonuisOP•4mo ago
didnt work
Pobiega
Pobiega•4mo ago
no spaces either
Alonuis
AlonuisOP•4mo 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];
}
}
}
}
}
Alonuis
AlonuisOP•4mo ago
anyway that's what im getting
No description
Pobiega
Pobiega•4mo ago
well yeah, your code only stores the single most repeated number, as far as I can tell
Alonuis
AlonuisOP•4mo ago
can we vc ill explain it
Pobiega
Pobiega•4mo ago
#vc-2
Jester
Jester•4mo 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•4mo 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();
Alonuis
AlonuisOP•4mo ago
@Pobiega i guess ur dictionary is the only way lol
Salman
Salman•4mo ago
:kekw:
Jester
Jester•4mo ago
skill issue you got close to mine
Salman
Salman•4mo ago
yeah yours was more concise and clever. I didn't think I could do this with grouping lol
Pobiega
Pobiega•4mo 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•4mo ago
LINQ is the beauty of C#. You would rarely find feature similar to LINQ in other langs lol
Pobiega
Pobiega•4mo 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•4mo ago
:kekw: this had me dying
Pobiega
Pobiega•4mo 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•4mo 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•4mo 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 😄
Alonuis
AlonuisOP•4mo ago
@Pobiega i fixed it
No description
Alonuis
AlonuisOP•4mo ago
it was j=i idk now it works had to use a copy u cant otherwise
Jester
Jester•4mo ago
i love groupby, very confusing but powerfull
Want results from more Discord servers?
Add your server