...

/

Solution Review: Finding the First Unique Integer in an Array

Solution Review: Finding the First Unique Integer in an Array

This review provides a detailed analysis of solving the "Finding the First Unique Integer in an Array" challenge.

We'll cover the following...

Solution: Brute force

Press + to interact
namespace chapter_2
{
class Challenge_6
{
//Find First Unique Integer in an Array
//Returns first unique integer from array
static int findFirstUnique(int []arr, int size)
{
//Inside the inner loop, check each index of the outer loop.
//If it's repeated in the array
//If it's not repeated then return this as the first unique integer
bool isRepeated = false;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (arr[i] == arr[j] && i != j)
{
isRepeated = true;
}
} //end of InnerLoop
if (isRepeated == false)
{
return arr[i];
}
isRepeated = false;
} //end of OuterLoop
return -1;
}
static void Main(string[] args)
{
int size = 6;
int []arr = { 2, 54, 7, 2, 6, 54 };
Console.Write( "Array: ");
for (int i = 0; i < size; i++)
Console.Write( arr[i] + " ");
Console.WriteLine("");
int unique = findFirstUnique(arr, size);
Console.WriteLine ("First Unique in an Array: " + unique );
return ;
}
}
}

Start from the first element, and traverse through the whole array by comparing it ...