• C# 找出数组中只出现了一次的数字


    .NET 生态越来越好,初学的朋友也越来越多。处理同一件简单的问题,随着我们知识的积累解决问题的方法也会越来越多。

    开始学习一门新的语言,我们经常会去解决之前用别的语言解决过无数次的老问题,今天我们来看看这么一道简单的查重题。

    题目

    c#输入十个数,找出其中所有只出现过一次的数字。

    题目分析

    让输入10个数字,这个很简单,控制台程序用 Console.ReadLine() 然后强制转换为 int。 最后让找出那个只出现了一次的元素,那么我们可以在输入过程中处理,也可以输入完成后处理,可以有以下解决方案。

    方法一

    首先我们介绍中规中矩的简单方法,涉及到 Dictionary 字典的用法。
    Dictionary的主要用途是提供快速的基于键值的元素查找。Dictionary的结构一般是这样的:Dictionary<[key], [value]>
    我们可以将输入的 int 为 key,出现的次数为 value,对每个输入的数字进行检索和计数,最终打印出只出现过一次的数据:

    Dictionary<int, int> input= new Dictionary<int, int>();
    for(int i = 0; i < 10; i++)
    {
        Console.Write($"请输入第{i+1}个数:");
        int temp = Convert.ToInt32(Console.ReadLine());
        // 如果存在要添加的
        if (input.ContainsKey(temp))
        {
            // 记录输入次数+1
            input[temp]++;
        }
        else
        {
            // 不存在计数1次
            input.Add(temp, 1);
        }
    }
    
    Console.WriteLine($"出现过一次的有:");
    foreach(var one in input)
    {
        if(one.Value == 1)
        {
            Console.WriteLine(one.Key);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    方法二

    我们可以使用 List 记录用户的输入,并同时在每次输入时查询之前是否已经输入过,若已经输入过则保存到另一个 List 中。最后比较两个 List 得出结论

    // 记录输入
    List<int> numbers = new List<int> { };
    // 记录重复的
    List<int> notthis = new List<int> { };
    for (int i = 0; i < 10; i++)
    {
        Console.Write($"请输入第{i + 1}个数:");
        int temp = Convert.ToInt32(Console.ReadLine());
        if (numbers.Contains(temp))
        {
            notthis.Add(temp);
        }
        numbers.Add(temp);
    }
    Console.WriteLine($"出现过一次的有:");
    foreach (int one in numbers)
    {
        if (!notthis.Contains(one))
        {
            Console.WriteLine(one);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    这段后面的部分可以使用 Except 进行差集计算优化为:

    List<int> haveone = numbers.Except(notthis).ToList();
    Console.WriteLine($"出现过一次的有:{string.Join(",", haveone)}");
    
    • 1
    • 2

    方法三

    我们也可以使用 Linq 来处理,先对其进行分组,然后查询出仅现过1次的数据。

    List<int> numbers = new List<int> { };
    for (int i = 0; i < 10; i++)
    {
        Console.Write($"请输入第{i + 1}个数:");
        int temp = Convert.ToInt32(Console.ReadLine());
        numbers.Add(temp);
    }
    
    var linquse = numbers.GroupBy(x => x)
        .Where(g => g.Count() == 1)
        .Select(s => s.Key);
    Console.WriteLine($"出现过一次的有:{string.Join(",", linquse)}");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    计算机网络网络层数据链路层协议详解
    5 个冷门但非常实用的 Kubectl 使用技巧,99% 的人都不知道
    CAN总线在OSI模型中层级
    QT关于界面常用设置
    Linux mdeltree命令教程:详解删除MS-DOS文件系统中的目录及其包含的所有文件和子目录的命令(附实例详解和注意事项)
    模拟问题(下)
    傻妞and青龙面板对接短信登录【2022.7.4】
    getid3 获取视频时长
    保姆级python安装教程
    在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
  • 原文地址:https://blog.csdn.net/marin1993/article/details/128123678