Advertisement
georgivorobyov

Array Majorant

Jan 5th, 2013
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class ArrayMajorant
  5. {
  6.     static int[] arrayMajorant(int[] array)
  7.     {
  8.         //Contain the number of different elements.
  9.         int numberOfDifferentElements = 0;
  10.         //First, allocate an array of size equal to the array we get.
  11.         //Every index will be pointer to another array with 2 indices.
  12.         //The first [0] will contain the number and the second [1] will contain its frequency.
  13.         int[][] lengthOfElements = new int[array.Length][];
  14.         for (int i = 0, length = array.Length; i < length; i++)
  15.         {
  16.             bool dontExist = true;
  17.             for (int j = 0; j < numberOfDifferentElements; j++)
  18.             {
  19.                 if (array[i] == lengthOfElements[j][0])
  20.                 {
  21.                     lengthOfElements[j][1]++;
  22.                     dontExist = false;
  23.                 }
  24.             }
  25.             //if current number do not exist
  26.             if (dontExist)
  27.             {
  28.                 lengthOfElements[numberOfDifferentElements] = new int[] { array[i], 1 };
  29.                 numberOfDifferentElements++;
  30.             }
  31.         }
  32.         //Let's find the largest number
  33.         int biggestIndex = 0;
  34.         for (int i = 0; i < numberOfDifferentElements; i++)
  35.         {
  36.             if (lengthOfElements[biggestIndex][1] < lengthOfElements[i][1])
  37.             {
  38.                 biggestIndex = i;
  39.             }
  40.         }
  41.         return lengthOfElements[biggestIndex];
  42.     }
  43.  
  44.     static int[] arrayMajorantDictionary(int[] array)
  45.     {
  46.         Dictionary<int, int> lengthOfElements = new Dictionary<int, int>();
  47.         for (int i = 0, length = array.Length; i < length; i++)
  48.         {
  49.             if (lengthOfElements.ContainsKey(array[i]))
  50.             {
  51.                 lengthOfElements[array[i]]++;
  52.             }
  53.             else
  54.             {
  55.                 lengthOfElements.Add(array[i], 1);
  56.             }
  57.         }
  58.         int maxKey = array[0];
  59.         foreach (var number in lengthOfElements)
  60.         {
  61.             if (number.Value > lengthOfElements[maxKey])
  62.             {
  63.                 maxKey = number.Key;
  64.             }
  65.         }
  66.         return new int[] { maxKey, lengthOfElements[maxKey] };
  67.     }
  68.  
  69.     static void Main()
  70.     {
  71.         int[] array = { 4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3};
  72.         //With Dictionary:
  73.         //int[] majorantDictionary = arrayMajorantDictionary(array);
  74.         //Console.WriteLine("{0} ({1} times)", majorantDictionary[0], majorantDictionary[1]);
  75.         //With Arrays:
  76.         int[] majorant = arrayMajorant(array);
  77.         Console.WriteLine("{0} ({1} times)", majorant[0], majorant[1]);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement