简体   繁体   English

如何使用内置函数显示列表中重复次数最少和最多的数字?

[英]How do you display the number that has the lowest and highest amount of duplicates in a list w/o using built in functions?

I've created a program that displays 1000 (1k) random integers with duplicates its rng range is from 1 - 1000, with that I want to know how many times a specific number has been generated with the highest and lowest frequency and display it.我创建了一个程序,它显示 1000 (1k) 个随机整数,其 rng 范围为 1 - 1000,我想知道以最高和最低频率生成特定数字的次数并显示它。 Ex: 51 is the number that has been generated 50 times, which is the highest (Note: I cannot use any built in function for logic building purposes)例如:51 是生成了 50 次的数字,这是最高的(注意:我不能使用任何内置函数来构建逻辑)

{
        List<int> numPool = new List<int>();
        Random rnd = new Random();

        string uinput = "";

        int storage = 0;

        while (true)
        {
            // generating number pool
            for (int i = 0; i < 1000; i++)
                numPool.Add(i + 1);

            // generating 100 numbers
            Console.WriteLine("Generating 100 numbers (1-1001) with duplicates...");
            int d = 1;
            while (numPool.Count > 0)
            {
                int temp = 0;

                temp = rnd.Next(0, numPool.Count); // this generates a random index between 0 and however long the list is
                Console.Write("{0}\t", temp);
                numPool.RemoveAt(temp); // removes the index
                if (d % 10 == 0)
                Console.WriteLine();

                d++;
            }

            Console.WriteLine("Highest amount of duplicate numbers: "); // this part should show the number with the highest amount of duplicates
            Console.WriteLine("Number of times it was duplicated: "); // this part will show how many times it was duplicated
            Console.WriteLine("\nLeast amount of Duplicate numbers: "); // this part should show the number with the least amount of duplicates
            Console.WriteLine("Number of times it was duplicated: "); // this part will show how many times it was duplicated

            Console.Write("\nWhat number would you like to show the frequency?: ");
            uinput = Console.ReadLine();
            storage = int.Parse(uinput);

            // from this part it should show the number of times the duplicate number has appeared according to the user input

            //Console.WriteLine("The number " + storage + " has appeared " + insert the amount of times it has appeared + " times.");


            Console.ReadKey();
            Console.Clear();
        }
    }

You could iterate through the list and pick one by one every number and then check if the number is repeated using a counter, when it finishes add the number to a new List so you have a list of already checked numbers that you can use For example: You have a List like this [1,1,5,1,7,9,9] First you'll pick the number 1 and check every element of the list if it's equals to 1 and if it is then you increment your counter.您可以遍历列表并逐个选择每个数字,然后使用计数器检查该数字是否重复,当它完成时将数字添加到新列表中,以便您拥有一个可以使用的已检查数字列表例如:你有一个这样的列表[1,1,5,1,7,9,9]首先你会选择数字 1 并检查列表中的每个元素是否等于 1,如果是,那么你增加你的柜台。 When it finishes you have to add the number 1 to a new list which we will call "alreadyChecked" just for the example and then we gotta have a variable for the minimum and another for the maximum and check if the counter is greater than the maximum and if it is lower of the minimum and assign it correctly.完成后,您必须将数字 1 添加到一个新列表中,我们将其称为“alreadyChecked”,仅用于示例,然后我们必须有一个变量作为最小值,另一个变量作为最大值,并检查计数器是否大于最大值如果它低于最小值并正确分配。 Now for the next number which is again 1 you'll check if this number is already present in the alreadyChecked list and if it is you just gotta go to the next number and so on.现在对于下一个数字,再次是 1,您将检查该数字是否已经存在于已经alreadyChecked的列表中,如果是,您只需转到下一个数字,依此类推。

Why no functions?为什么没有函数? Store the results in a table and use the SQL count(), and group by functions to give a count of every occurrence of any given number.将结果存储在一个表中并使用 SQL count() 和 group by 函数来对任何给定数字的每次出现进行计数。 If you sort descending your highest counts will appear first.如果您按降序排序,您的最高计数将首先出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM