Wednesday, September 19, 2018

Array Mode

  • Problem Description

    Write a program to find the mode of the elements in the array.

    The mode in a list of numbers refers to the list of numbers that occur most frequently. It is important to note that there can be more than one mode and if no number occurs more than once in the set, then there is no mode for that set of numbers.



    Input and Output Format:

    Input consists of n+1 integers where n corresponds to the number of elements in the array.

    The first integer corresponds to n and the next n integers correspond to the elements in the array.

    Refer sample input and output for formatting specifications.

    Assume that the maximum number of elements in the array is 20.

    Assume that in the input dataset there is 1 mode or no mode at all.

    All text in bold corresponds to input and the rest corresponds to output.
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int a[100],i,j,n,flag=0;
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
        scanf("%d",&a[i]);
      }
      for(i=0;i<n;i++)
      {
        for(j=i+1;j<n;j++)
        {
          if(a[j]==a[i])
          {
            flag=1;
          printf("Mode=%d ",a[i]);
        }
      }
      }
      if(flag==0)
        printf("none");
     return 0;
    }
  • Test Case 1

    Input (stdin)
    5
    
    2 4 2 3 5
    
    
    Expected Output
    Mode=2
  • Test Case 2

    Input (stdin)
    5
    
    2 4 1 3 5
    
    
    Expected Output
    none

No comments:

Post a Comment