#Find most frequent element in an array

5 messages · Page 1 of 1 (latest)

strange yoke
#

Hey guys im pretty new to c and im unable to reduce the number of conditionals and loops in this thing, i would greatly appreciate if you could share some better approach to this

arctic peakBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

strange yoke
#
#include <stdio.h>
int
main ()
{
  int FreqArr[100000];
  int Size;

  scanf ("%d", &Size);
  int distinct[Size];
  for (int i = 0; i < Size; i++)
    {
      scanf ("%d", &FreqArr[i]);
    }
  int flag = 1, c = 0;
  for (int i = 0; i < Size; i++)
    {
      for (int j = 0; i < Size; i++)
    {
      if (FreqArr[i] == distinct[j])
        {
          flag = 0;
          break;
        }
      if (flag == 1)
        {
          distinct[c] = FreqArr[i];
          c += 1;
        }
    }
    }
  int count = 0, greatest = 0, greatest_num = 0;
  for (int i = 0; i < c; i++)
    {
      for (int j = 0; j < Size; j++)
    {
      if (distinct[i] == FreqArr[j])
        {
          count += 1;
        }
      if (count > greatest)
        {
          printf ("greatest changes:%d") greatest = count;
          greatest_num = distinct[i];
        }
      else if (count == greatest && greatest_num > FreqArr[i])
        {
          greatest_num = distinct[i];
        }

    }
    }
  printf ("%d", greatest_num);
  return 0;
}```
#

something more readable

#

i would appreciate any sort of help