如何判定字符串或字符串数组中是否存在重复的项呢?在项目开发中经常需要,下面讲解一下
目录
字符串重复判定
public static bool HasDuplicateChars(string str)
{
bool[] charSet = new bool[128];
foreach (char c in str)
{
if (charSet[c])
return true;
charSet[c] = true;
}
return false;
}
字符串数组判定
bool hasRepeat(string[] strs)
{
for (int i = 1; i < strs.Length; i++)
for (int j = 0; j < i; j++)
{
if (strs[j] == strs[i])
return true;
}
return false;
}