C
C#13mo 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
TheRanger13mo 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
MODiX13mo 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
TheRanger13mo ago
or you can make the class contain a generic argument
MODiX
MODiX13mo 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
TheRanger13mo 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
JibzOP13mo 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
JibzOP13mo ago
but then, I kinda just converted to string and store it as <string, int> like key = string.Format()
exokem
exokem13mo 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
jcotton4213mo ago
As the arrays appear to be a fixed size, I would just use ValueTuple
Jibz
JibzOP13mo ago
yeah that could work too. But using dictionary was just my first thought
Accord
Accord13mo 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