TDD
FizzBuzz & Duplicates
FizzBuzz & Duplicates
  • Fizz Buzz Kata
  • Finding Duplicate CSharp
  • Finding Duplicate Java
  • Prime Number Exercise
Powered by GitBook
On this page
  • Finding Duplicate: A TDD Exercise
  • The problem
  • Approach using TDD
  • Some snippets to get you started

Finding Duplicate CSharp

Finding Duplicate: A TDD Exercise

The problem

Given an integer array of any size, we need to find if it contains duplicate elements or not, without using any built-in functions specifically for that and also by not using any data structures other than arrays!

Approach using TDD

  1. Start with a small and naive failing test, say for just 2 elements

  2. Code just enough to pass it

  3. Refactor if you can

  4. Move ahead with a less naive failing test, this time with 3 elements

  5. Code just enough to pass it

  6. Refactor

Repeat the same until most non-trivial cases with more elements are passing. Once we have enough cases, we could also see at improving the code or doing it in more efficient ways!

Some snippets to get you started

Snippet 1

return array[0] == array[1];

Snippet 2

return (array[0] == array[1] || array[0] == array[2]);

Snippet 3

bool res = false;
for(int i = 0; i < array.Length; i++)
{
	for(int j=i+1; j < array.Length-1; j++)
	{
		if(array[i] == array[j])
			res = true;
	}
}
return res;

Do you see any issue with above code? :O

Exercise: Try this out by using the partially completed solution:

PreviousFizz Buzz KataNextFinding Duplicate Java

Last updated 1 year ago

GitHub - raviramorg/Duplicates: Given an integer array of any size, we need to find if it contains duplicate elements or notGitHub
Logo