在一個字符串數組中找出重複的字符串(C#)

方法一:(hashtable)推薦使用
private bool checkImportDuplicate(string[] str )
{
bool flag = false;
Hashtable hash = new Hashtable();
for (int i = 0; i < str.Length; i++)
{
if (!hash.ContainsKey(str[i]))
{
hash.Add(str[i], i);
cntIntLists[i].Add(i);//全局變量List[] cntIntLists = new List[N];記錄重複的字符串的下標值
}
else
{
int value = (int)hash[str[i]];
cntIntLists[value].Add(i);
flag = true;
}
}
return flag;
}
方法二:(雙重循環)
private bool checkImportDuplicate(string[] str )
{
bool flag = false;
bool[] f=new bool[str.Length];
for (int i = 0; i < str.Length; i++)
{
f[i] = true;
}
List[] cnt=new List[];
for (int i = 0; i < str.Length-1&&f[i]; i++)
{
cntIntLists[i].Add(i);//全局變量List[] cntIntLists = new List[N];記錄重複的字符串的下標值
for (int j = i + 1; j < str.Length&&f[j]; j++)
{
if (str[i].Equals(str[j]))
{
cntIntLists[i].Add(j);
flag = true;
f[i] = false;
}
}
}
return flag;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章