using System.Collections.Generic;
private static List<int> Demo(int[] myArray) {
for (int i = 1; i < myArray.Length; ++i)
if (myArray[i - 1] == myArray[i])
int right = myArray.Length - 1;
for (int i = myArray.Length - 2; i >= 0; --i)
if (myArray[i + 1] == myArray[i])
List<int> myList = myArray
private static List<int> Demo2(int[] myArray) {
HashSet<int> remove = new HashSet<int>();
if (myArray.Length > 1) {
if (myArray[0] == myArray[1])
if (myArray[myArray.Length - 1] == myArray[myArray.Length - 2])
remove.Add(myArray[myArray.Length - 1]);
.Where(item => !remove.Contains(item))
public static void Main()
int[][] tests = new int[][] {
new int[] {1, 1, 2, 2, 3, 4, 5, 5, 9, 9 },
new int[] {1, 1, 2, 3, 1, 1, 8, 9, 9, 4, 5, 9, 9},
new int[] {1, 1, 2, 3, 1, 1, 8, 8, 9, 9, 4, 5, 9, 9},
new int[] {1, 1, 2, 3, 1, 8, 8, 9, 4, 5, 9, 9},
new int[] {1, 1, 1, 2, 2},
new int[] {1, 1, 1, 2, 2, 2, 2},
new int[] {1, 2, 3, 4, 5}
var result = string.Join(Environment.NewLine, tests
.Select(test => $" [{string.Join(", ", test)}] => [{string.Join(", ", Demo(test))}]"));
Console.WriteLine("Spare:");
Console.WriteLine(result);
Console.WriteLine("Don't Spare:");
result = string.Join(Environment.NewLine, tests
.Select(test => $" [{string.Join(", ", test)}] => [{string.Join(", ", Demo2(test))}]"));
Console.WriteLine(result);