Академия Телерик – Лекция 2 – Масиви – Домашно – Задача 09

Пробвах да използвам възможността да съкратя времето за минаване през масива, като премахвам елементите, които вече са проверени. Получи се…:)

/*
 * Write a program that finds the most frequent number in an array. Example:
    {4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} –> 4 (5 times)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E09_FindMostFrequentNumber
{
    class E09_FindMostFrequentNumber
    {
        static void Main(string[] args)
        {
            //decleare the variables to be used
            bool isNum = false;
            int n = 0, temp = 0, val = 0, valCount = 0, tempVal = 0, tempCount = 0;

            List<int> myArr = new List<int>();

            //start initializing the variables needed
            //get the length of the list from the console
            Console.WriteLine(“Please enter N:”);
            while (!isNum)
            {
                isNum = int.TryParse(Console.ReadLine(), out n);
                if (!isNum)
                {
                    Console.WriteLine(“Please insert a valid number”);
                }
            }

            //insert values in the list
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine(“Please enter element {0}:”, i + 1);
                isNum = false;
                while (!isNum)
                {
                    isNum = int.TryParse(Console.ReadLine(), out temp);
                    if (!isNum)
                    {
                        Console.WriteLine(“Please insert a valid number”);
                    }
                    else
                    {
                        myArr.Add(temp);
                    }
                }
            }

            //start checking the array for the most frequent number
            while (myArr.Count > 0)
            {
                //use a temporary list in order to save processing time
                List<int> tempArr = myArr.ToList();
                tempVal = tempArr[0];
                tempCount = 0;
                for (int j = 0; j < tempArr.Count; j++)
                {
                    Console.WriteLine(tempArr[j]);
                    if (tempArr[j] == tempVal)
                    {
                        myArr.Remove(tempArr[j]);
                        tempCount++;
                    }
                }
                if (tempCount > valCount)
                {
                    valCount = tempCount;
                    val = tempVal;
                    Console.WriteLine(tempVal + ” is present ” + tempCount + “times”);
                }
            }

            //print the result of the checks
            Console.WriteLine(“The most frequent element is {0}”, val);
            Console.WriteLine(“Its is present {0} times in the array.”, valCount);  
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>