-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Lieu
committed
Dec 15, 2021
1 parent
792e2b7
commit 581d6fe
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
HighestRankedNumber/HighestRankedNumber.Tests/HighestRankedNumber.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\HighestRankedNumber\HighestRankedNumber.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
36 changes: 36 additions & 0 deletions
36
HighestRankedNumber/HighestRankedNumber.Tests/HighestRankedNumberTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Xunit; | ||
|
||
namespace HighestRankedNumber.Tests | ||
{ | ||
public class HighestRankedNumberTests | ||
{ | ||
[Fact] | ||
public void Test_HighestRank() | ||
{ | ||
var arr = new int[] { 12, 10, 8, 12, 7, 6, 4, 10, 12 }; | ||
Assert.Equal(12, HighestRankedNumber.HighestRank(arr)); | ||
} | ||
|
||
[Fact] | ||
public void Test_HighestRank2() | ||
{ | ||
// { 12: 3, 10: 2, 8: 1, 7: 1, 6: 1, 4: 1 } | ||
var arr = new int[] { 12, 10, 8, 12, 7, 6, 4, 10, 12, 10 }; | ||
Assert.Equal(12, HighestRankedNumber.HighestRank(arr)); | ||
} | ||
|
||
[Fact] | ||
public void Test_HighestRank3() | ||
{ | ||
var arr = new int[] { 12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10 }; | ||
Assert.Equal(3, HighestRankedNumber.HighestRank(arr)); | ||
} | ||
|
||
[Fact] | ||
public void Test_HighestRank4() | ||
{ | ||
var arr = new int[] { 6, 7, 10 }; | ||
Assert.Equal(10, HighestRankedNumber.HighestRank(arr)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31919.166 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HighestRankedNumber", "HighestRankedNumber\HighestRankedNumber.csproj", "{5EFEFED8-7E21-47FA-A719-162F3EE7384A}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HighestRankedNumber.Tests", "HighestRankedNumber.Tests\HighestRankedNumber.Tests.csproj", "{1C36AD8C-F29F-44E4-BC80-FEE34D5771C7}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{5EFEFED8-7E21-47FA-A719-162F3EE7384A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{5EFEFED8-7E21-47FA-A719-162F3EE7384A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{5EFEFED8-7E21-47FA-A719-162F3EE7384A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{5EFEFED8-7E21-47FA-A719-162F3EE7384A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{1C36AD8C-F29F-44E4-BC80-FEE34D5771C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{1C36AD8C-F29F-44E4-BC80-FEE34D5771C7}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{1C36AD8C-F29F-44E4-BC80-FEE34D5771C7}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{1C36AD8C-F29F-44E4-BC80-FEE34D5771C7}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {F19F308C-622D-4A30-863A-86AE4E90CCAF} | ||
EndGlobalSection | ||
EndGlobal |
39 changes: 39 additions & 0 deletions
39
HighestRankedNumber/HighestRankedNumber/HighestRankedNumber.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace HighestRankedNumber | ||
{ | ||
public class HighestRankedNumber | ||
{ | ||
public static int HighestRank(int[] arr) | ||
{ | ||
Array.Sort(arr); | ||
var dict = new Dictionary<int, int>(); | ||
var highestCount = 0; | ||
var highestRank = 0; | ||
var highestNoDups = 0; | ||
for (int i = 0; i < arr.Length; i++) | ||
{ | ||
if (dict.ContainsKey(arr[i])) | ||
{ | ||
var currentCount = dict[arr[i]] + 1; | ||
dict[arr[i]] = currentCount; | ||
if (currentCount >= highestCount) | ||
{ | ||
highestCount = currentCount; | ||
highestRank = highestRank > arr[i] | ||
? highestRank | ||
: arr[i]; | ||
} | ||
} | ||
else | ||
{ | ||
dict.Add(arr[i], 1); | ||
if (arr[i] > highestNoDups) | ||
highestNoDups = arr[i]; | ||
} | ||
} | ||
|
||
return highestRank > 0 | ||
? highestRank | ||
: highestNoDups; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
HighestRankedNumber/HighestRankedNumber/HighestRankedNumber.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |