Commit 4b1394c5 authored by Dmytro Bogatov's avatar Dmytro Bogatov 💕
Browse files

Add Lonely Integer.

parent b318683f
namespace CodingInterview.HackerRank
{
/// <summary>
/// You will be given an array of integers.
/// All of the integers except one occur twice.
/// That one is unique in the array.
///
/// Given an array of integers, find and print the unique element.
///
/// For example, a = [1, 2, 3, 4, 3, 2, 1], the unique element is 4.
/// </summary>
public class LonelyInteger
{
/// <summary>
/// Finds the unique element
/// </summary>
/// <param name="a">An array of integers where each integer occurs twice except one, which occurs once</param>
/// <returns>The integer which occurs once</returns>
public long Solve(int[] a)
{
for (var i = 1; i < a.Length; i++)
{
a[0] = a[0] ^ a[i];
}
return a[0];
}
}
}
using Xunit;
namespace CodingInterview.Tests.HackerRank
{
public class LonelyInteger
{
[Fact]
public void TestCases()
{
// From Hacker Rank
Assert.Equal(4, new CodingInterview.HackerRank.LonelyInteger().Solve(new int[] { 1, 2, 3, 4, 3, 2, 1 }));
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment