判断数组有没有重复值

mike086 2010-07-27 03:21:10
一串字符 a;b;v;n;mm;tt;yy;b;y;ca;a以分号分隔。

如何快速简单判断其中有没重复值。用net2.0
...全文
799 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
riyuetaiyangxing 2010-07-28
  • 打赏
  • 举报
回复
string[] com = {"a", "b", "v", "n", "mm", "tt", "yy", "b", "y", "ca", "a" };
for (int i = 0; i < com.Length;i++)
{
for (int j = i; j < com.Length; j++)
{
if(com[i] = com[j])
{
Console.WriteLine("有重复值");
}
}
}
我这段代码的思路:
首先将这个数组的第一元素与其他元素一一比较,如果有重复值,就输出
然后将第二元素与其他元素一一比较,如果有重复值,就输出
依次下去
leiziaitudou 2010-07-28
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string s = "a;b;v;n;mm;tt;ttt;f;y;ca;c";
string[] s1 = s.Split(';');


for (int i = 0; i < s1.Length; i++)
{
for (int j = 0; j < s1.Length; j++)
{
if (i!=j)
{
if (s1[i] == s1[j])
{
Console.WriteLine("有重复");
return;
}
}
}
}
}
}
}
loveSoftandhxy 2010-07-28
  • 打赏
  • 举报
回复
存入hashTable,包含相同的的值,重复。。。
string a = "a,b,c,d,ff,ee,rrrrr,ffffffff,a,b";
Hashtable tb = new Hashtable();
for(int i=0;i<a.Split(',').Length;i++){
tb.Add(i,a.Split(',')[i]);
if (tb.ContainsValue(a.Split(',')[a.Split(',').Length - i-1]))
{
Console.Write("重复!!");
}

只是一种想法。
dodducs 2010-07-28
  • 打赏
  • 举报
回复
使用 HashTable 或者 Dictionary 都可以,,只要出现重复值 他是不会让你作为主键的
你可以通过 try catch 捕获异常判断 或者是使用 !Containt 就作为主键 包括就扔出去 有重复即可
LutzMark 2010-07-28
  • 打赏
  • 举报
回复
如果判断有没重复字符

class Program
{
static void Main(string[] args)
{
Console.WriteLine(CharRepeatCheck("a;b;v;n;mm;tt;yy;b;y;ca;a") ? "有重复的字符" : "没有重复字符");
}
static bool CharRepeatCheck(string value)
{
char[] values = value.Replace(";","").ToCharArray();
int a=values.Distinct().Count();
HashSet<string> hs = new HashSet<string>();
foreach (char entity in values)
{
if (!hs.Add(entity.ToString()))
{
return true;
}
}
return false;
}
}
LutzMark 2010-07-28
  • 打赏
  • 举报
回复
class Program
{
static void Main(string[] args)
{
Console.WriteLine( StringRepeatCheck("a;b;v;n;mm;tt;yy;b;y;ca;a")?"有重复的":"没有重复");
}
static bool StringRepeatCheck(string value)
{
string[] values = value.Split(';');
HashSet<string> hs = new HashSet<string>();
foreach (string entity in values)
{
if (!hs.Add(entity.ToString()))
{
return true;
}
}
return false;
}
}
-过客- 2010-07-28
  • 打赏
  • 举报
回复
如果只是判断是否有重复值

string test = "a;b;v;n;mm;tt;yy;b;y;ca;a";
Regex reg = new Regex(@"(?s)(?<=^|;)([^;]+)(?=;).*?(?<=;)\1(?=;|$)");
if (reg.IsMatch(yourStr))
{
richTextBox2.Text = "有重复值!";
}
else
{
richTextBox2.Text = "没有重复值!";
}
iammac 2010-07-28
  • 打赏
  • 举报
回复
不用Linq的话

string[] ary = { "a","b","c","a"};
Dictionary<string, string> dict = new Dictionary<string, string>();
try
{
foreach (var s in ary)
{
dict.Add(s, null);
}
}
catch (Exception e)
{
Console.WriteLine("存在相同的值");
}
weiwolong 2010-07-27
  • 打赏
  • 举报
回复
string[] com = new string[] { "a", "b", "v", "n", "mm", "tt", "yy", "b", "y", "ca", "a" };
if (com.Distinct().Count() != com.Length)
{
MessageBox.Show("有重复的字符串!");
}
justfortemp 2010-07-27
  • 打赏
  • 举报
回复
这个只能split后两两比较了
水哥阿乐 2010-07-27
  • 打赏
  • 举报
回复
string source = "a;b;v;n;mm;tt;yy;b;y;ca;a";
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("\\w+");
List<string> Arrlist = new List<string>();
System.Text.RegularExpressions.MatchCollection mc = re.Matches(source);
foreach (Text.RegularExpressions.Match m in mc) {
//Console.WriteLine(m.Value)
if (!Arrlist.Contains(m.ToString)) {
Arrlist.Add(m.ToString);
Console.WriteLine(m.ToString);
}
}

Console.ReadLine();
JiaJiaHappyForever 2010-07-27
  • 打赏
  • 举报
回复
存到数组中 distinct
宇峰科技 2010-07-27
  • 打赏
  • 举报
回复
先用split分隔,存入数组,然后排序,再然后两两比较
t20100504t 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 myhope88 的回复:]
嗯,先放到数组中,然后排序,再然后两两比较
[/Quote]
up
myhope88 2010-07-27
  • 打赏
  • 举报
回复
嗯,先放到数组中,然后排序,再然后两两比较
mike086 2010-07-27
  • 打赏
  • 举报
回复
不用linq 。
wuyq11 2010-07-27
  • 打赏
  • 举报
回复
LIST.Contains
chokobo 2010-07-27
  • 打赏
  • 举报
回复
先排序,之后两两比较。
Aquarius娜吖 2010-07-27
  • 打赏
  • 举报
回复
distinct
wuyq11 2010-07-27
  • 打赏
  • 举报
回复
linq DISTINCT

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧