Skip to content

Commit 218cab1

Browse files
committed
Added second test for duplicates
1 parent de858a7 commit 218cab1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

CodingInterview/CodingInterview/Tests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ int[] RemoveDuplicates(int[] arrayOfNumbers)
1616
return numbers.ToArray();
1717
}
1818

19+
int[] LeaveTwoDuplicates(int[] arrayOfNumbers)
20+
{
21+
var numbers = new HashSet<int>();
22+
var numbersWithTwoDuplicates = new List<int>();
23+
for (int index = 0; index < arrayOfNumbers.Length; index++)
24+
{
25+
numbers.Add(arrayOfNumbers[index]);
26+
}
27+
28+
for (int index = 0; index < numbers.Count; index++)
29+
{
30+
var actualValue = numbers.ElementAt(index);
31+
if (arrayOfNumbers.Where(element => element == actualValue).Count() >= 2)
32+
{
33+
numbersWithTwoDuplicates.Add(actualValue);
34+
}
35+
numbersWithTwoDuplicates.Add(actualValue);
36+
}
37+
return numbersWithTwoDuplicates.ToArray();
38+
}
39+
1940
[Test]
2041
public void RemoveDuplicatesFromSortedArray_1()
2142
{
@@ -26,5 +47,16 @@ public void RemoveDuplicatesFromSortedArray_1()
2647

2748
Assert.AreEqual(expectedSortedArrayWithoutDuplicates, sortedArrayWithoutDuplicatesCalculated);
2849
}
50+
51+
[Test]
52+
public void RemoveDuplicatesFromSortedArray_2()
53+
{
54+
var sortedArrayWithDuplicates = new int[] { 1, 3, 3, 3, 6, 8, 8, 9, 9, 9 };
55+
var expectedSortedArrayWithTwoDuplicates = new int[] { 1, 3, 3, 6, 8, 8, 9, 9 };
56+
57+
var sortedArrayWithTwoDuplicatesCalculated = LeaveTwoDuplicates(sortedArrayWithDuplicates);
58+
59+
Assert.AreEqual(expectedSortedArrayWithTwoDuplicates, sortedArrayWithTwoDuplicatesCalculated);
60+
}
2961
}
3062
}

0 commit comments

Comments
 (0)