C
C#9mo ago
Jibz

❔ How do I check for an array key in Dictionary?

Dictionary<int[], int> test =
new()
{
{ new int[] { 2, 3 }, 3 }, // <- this one
{ new int[] { 3, 3 }, 6 },
{ new int[] { 3, 2 }, 3 }
};

Console.WriteLine(test.ContainsKey(new int[] { 2, 3 }));
Dictionary<int[], int> test =
new()
{
{ new int[] { 2, 3 }, 3 }, // <- this one
{ new int[] { 3, 3 }, 6 },
{ new int[] { 3, 2 }, 3 }
};

Console.WriteLine(test.ContainsKey(new int[] { 2, 3 }));
output:
False
11 Replies
TheRanger
TheRanger9mo ago
yeah you need to create a class that implements IEqualityComparer and do the logic to make it equal if the elements are the same and inject an instance of that class to the Dictionary's constructor or perhaps there is a built in class that already does that
MODiX
MODiX9mo ago
TheRanger
REPL Result: Success
class IntArrayComparer : IEqualityComparer<int[]>
{
public bool Equals(int[] array, int[] other)
{

return array.SequenceEqual(other);
}

public int GetHashCode(int[] array)
{
return array.Length;
}
}

// your code
Dictionary<int[], int> test =
new(new IntArrayComparer())
{
{ new int[] { 2, 3 }, 3 }, // <- this one
{ new int[] { 3, 3 }, 6 },
{ new int[] { 3, 2 }, 3 }
};

Console.WriteLine(test.ContainsKey(new int[] { 2, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 2, 4 }));
Console.WriteLine(test.ContainsKey(new int[] { 2, 4, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 3, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 3, 2 }));
class IntArrayComparer : IEqualityComparer<int[]>
{
public bool Equals(int[] array, int[] other)
{

return array.SequenceEqual(other);
}

public int GetHashCode(int[] array)
{
return array.Length;
}
}

// your code
Dictionary<int[], int> test =
new(new IntArrayComparer())
{
{ new int[] { 2, 3 }, 3 }, // <- this one
{ new int[] { 3, 3 }, 6 },
{ new int[] { 3, 2 }, 3 }
};

Console.WriteLine(test.ContainsKey(new int[] { 2, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 2, 4 }));
Console.WriteLine(test.ContainsKey(new int[] { 2, 4, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 3, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 3, 2 }));
Console Output
True
False
False
True
True
True
False
False
True
True
Compile: 781.850ms | Execution: 120.559ms | React with ❌ to remove this embed.
TheRanger
TheRanger9mo ago
or you can make the class contain a generic argument
MODiX
MODiX9mo ago
TheRanger
REPL Result: Success
class ArrayComparer<T> : IEqualityComparer<T[]>
{
public bool Equals(T[] array, T[] other)
{
return array.SequenceEqual(other);
}


public int GetHashCode(T[] array)
{
return array.Length;
}
}
//your code
Dictionary<int[], int> test =
new(new ArrayComparer<int>())
{
{ new int[] { 2, 3 }, 3 }, // <- this one
{ new int[] { 3, 3 }, 6 },
{ new int[] { 3, 2 }, 3 }
};

Console.WriteLine(test.ContainsKey(new int[] { 2, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 2, 4 }));
Console.WriteLine(test.ContainsKey(new int[] { 2, 4, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 3, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 3, 2 }));
class ArrayComparer<T> : IEqualityComparer<T[]>
{
public bool Equals(T[] array, T[] other)
{
return array.SequenceEqual(other);
}


public int GetHashCode(T[] array)
{
return array.Length;
}
}
//your code
Dictionary<int[], int> test =
new(new ArrayComparer<int>())
{
{ new int[] { 2, 3 }, 3 }, // <- this one
{ new int[] { 3, 3 }, 6 },
{ new int[] { 3, 2 }, 3 }
};

Console.WriteLine(test.ContainsKey(new int[] { 2, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 2, 4 }));
Console.WriteLine(test.ContainsKey(new int[] { 2, 4, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 3, 3 }));
Console.WriteLine(test.ContainsKey(new int[] { 3, 2 }));
Console Output
True
False
False
True
True
True
False
False
True
True
Compile: 754.357ms | Execution: 138.209ms | React with ❌ to remove this embed.
TheRanger
TheRanger9mo ago
but the question is, why Dictionary<int[], int> ? if you will tell me what are you trying to achieve, i may provide a better solution
Jibz
Jibz9mo ago
i was trying this problem https://leetcode.com/problems/unique-paths/ and I wanted to solve this with DP so I wanted to store the grid as key and the result as value
LeetCode
Unique Paths - LeetCode
Can you solve this real interview question? Unique Paths - There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, ...
Jibz
Jibz9mo ago
but then, I kinda just converted to string and store it as <string, int> like key = string.Format()
exokem
exokem9mo ago
What is the effect of storing the grid as the key? There is only ever one grid and I wouldn't expect any cell to have state your end goal is to determine the number of possible routes to get from a point A to a point B so you could try instead tracking the number of possible routes to get from A to each other point in the grid (the subproblem for a given cell is the number of routes from A to that cell) then your solution is the value assigned to the point B in this case you would only need one 2d array, or even a map with points as keys
jcotton42
jcotton429mo ago
As the arrays appear to be a fixed size, I would just use ValueTuple
Jibz
Jibz9mo ago
yeah that could work too. But using dictionary was just my first thought
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
✅ Is use of IDisposable, using keyword for a UI wait indicator incorrect?I have a wait indicator for WPF that implements `IDisposable`. When creating a new object of this `I✅ Why do my ASP.NET Core Requests not run in parallel?```csharp var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.MapGet("/❔ Help with Duende Login for part of pageI am having an issue that I have been struggeling with for quite some time now. I want only part of ❔ Validation Error auto resize parentHow to auto resize parent when has validation error? So that the error text doesn't crash into other❔ ✅ Coding Test Website in ASP.NETSo im making a website that should be able to login users (from school server) and Admin/Reader shou❔ Learning platformI'm currently learning C# at CS education. It's going great, but as we're dipping our toes into abst❔ guidance on protecting sensitive details (licence details) neededQ: does anyone have any guidance on protecting sensitive details (licence name and serial) from bein❔ Convert .docx to .pdf without restrictionsI need to make a .pdf from a .docx, but there is a problem I have been looking at libraries and they❔ JWT + Microsoft.AspNetCore.Authentication.JwtBearerI added to my small (Asp.net Webapi) project JSON Web Token from tutorial but I don't uderstand thi❔ Question about Azure AD B2C and Front-End Authentication via APIHey everyone! I've got a question about Azure AD B2C. After someone signs up using the "RegisterPort