C# 判断List集合中是否有重复的项

/*在.Net 3.5 以上*/
bool HaveDuplicates = vList.GroupBy(i => i).Where(g => g.Count() > 1).Count() >= 1;

 

转载于:https://www.cnblogs.com/ouylvr0625/p/8087177.html

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断JSON数据是否重复,可以按照以下步骤进行: 1. 读取JSON文件,并将JSON数据解析为一个对象数组。 2. 对于每个JSON对象,将其转换为字符串,并计算其哈希值(可以使用C#的SHA256Managed类),将哈希值存储到一个HashSet。 3. 如果HashSet已经存在该哈希值,则说明该JSON数据是重复的。 示例代码: ```csharp using Newtonsoft.Json; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Text; public static bool HasDuplicateJson(string filePath) { HashSet<string> hashSet = new HashSet<string>(); using (StreamReader reader = new StreamReader(filePath)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); object jsonObject = JsonConvert.DeserializeObject(line); string jsonString = JsonConvert.SerializeObject(jsonObject, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); byte[] jsonData = Encoding.UTF8.GetBytes(jsonString); byte[] hashData = new SHA256Managed().ComputeHash(jsonData); string hashString = Encoding.UTF8.GetString(hashData); if (hashSet.Contains(hashString)) { return true; } hashSet.Add(hashString); } } return false; } ``` 该函数接受一个文件路径作为输入,返回一个布尔值,表示文件是否存在重复的JSON数据。函数内部遍历文件的每一行JSON数据,并计算其哈希值,判断是否存在重复。函数可以根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值