Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd7725d

Browse files
committed
Added contains duplicate test
1 parent 7b130e5 commit bd7725d

File tree

1 file changed

+29
-0
lines changed
  • CodingInterview/CodingInterview

1 file changed

+29
-0
lines changed

‎CodingInterview/CodingInterview/Tests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,23 @@ private static string GetAlphabeticString(string message)
186186
return alphabeticMessageString;
187187
}
188188

189+
private static bool CheckIfArrayContainsDuplicatedElements(int[] inputArray)
190+
{
191+
if (inputArray == null || inputArray.Length == 0)
192+
throw new ArgumentNullException(nameof(inputArray), "inputArray needs to have elements");
193+
194+
var hashSet = new HashSet<int>();
195+
foreach (var element in inputArray)
196+
{
197+
if (hashSet.Contains(element))
198+
{
199+
return true;
200+
}
201+
hashSet.Add(element);
202+
}
203+
return false;
204+
}
205+
189206
[Test]
190207
public void RemoveDuplicatesFromSortedArray_1()
191208
{
@@ -312,5 +329,17 @@ public void ValidPalindrome_22()
312329
//then
313330
Assert.AreEqual(true, isPalindrome);
314331
}
332+
333+
[Test]
334+
[TestCase(new[] { 0, 1, 2, 4, 4, 5, 7 }, true)]
335+
[TestCase(new[] { 0, 1, 2, 4, 5, 7 }, false)]
336+
public void ContainsDuplicates_82(int []inputArray, bool isDuplicateElement)
337+
{
338+
//given
339+
//when
340+
var isPalindrome = CheckIfArrayContainsDuplicatedElements(inputArray);
341+
//then
342+
Assert.AreEqual(isDuplicateElement, isPalindrome);
343+
}
315344
}
316345
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /