私は問題を解決しようとしていました。
基本的に、文字配列から 2 番目の複製を選択する必要があります。
Input {'x','y','z','x','y'} output: y
Input { 'a', 'a', 'b', 'a', 'c', 'b', 'a', 'c', 'b' } Output: b
Input { 'a','a','a','b','a','c','b','a','c','b' } output: b
編集:
Input {'a', 'b', 'c', 'b', 'a', 'c', 'b', 'a', 'c', 'b'} Output: a
このコードを書いてみましたが、最初の文字がすぐに繰り返されると失敗します:(これを修正する助けはありますか?
public static char returnSecondDuplicate(char[] arr)
{
if (arr.Length == 0)
throw new ArgumentNullException("empty input");
var dictionary = new Dictionary<char, int>();
Char second = '\0';
int duplicateCount = 0;
for (int i = 0; i <= arr.Length - 1; i++)
{
if (!dictionary.ContainsKey(arr[i]))
{
dictionary.Add(arr[i], 1);
}
else
{
duplicateCount++;
if (duplicateCount == 2)
{
second = arr[i];
}
}
}
return second;
}