C# find matching pairs of arrays (when items inside are mixed) - c#

I would like to ask if there is a data structure or some sort of dictionary that would help me to solve this problem.
First I create array of arrays of int.
11,32,21,10;
455,476,465,454;
11,32,476,455;
...
10,32,21,11;
Besides that I am adding points to separate array of arrays
Pt11,Pt32,Pt21,Pt10;
Pt455,Pt476,Pt465,Pt454;
Pt11,Pt32,Pt476,Pt455;
...
Pt10,Pt32,Pt21,Pt11;
Is there a way to create a dictionary to add array of points by name, and the name is array of integers - 10,32,21,11 - .
But the problem I have, is that I want to add to the same dictionary point array if name is mixed - 11,32,21,10.
So dictionary would point to the same collection - if I call 10,32,21,11 or 11,32,21,10. In other words 10,32,21,11 and 11,32,21,10 is the same name because elements are just ordered differently.
I do not know if it clear as I mixing several things:
1. Is it possible to create at least a dictionary whose name is an array?
2. Then if yes it probably would not point to the same points array if I add elements dictionary.Add(10,32,21,11, pt[]); dictionary.Add(11,32,21,10, pt[]);

Related

How to use SkipWhile() to skip duplicate elements in two arrays?

I am trying to get a list of numbers (upto 9) that don't contain any numbers already in a 3x3 cell from which is contained within a 81 large 2d array of integers. I'm attempting to make sudoku. There are likely better solutions that have nothing to with skipwhile or something similar, but they all seem to rely on duplicate numbers having the same index in both array which doesn't work.
int[] inCellNumbers = thisCell.Intersect(Numbers).ToArray();
int[] availableNumbers = Numbers.SkipWhile(Numbers.Contains<int>(inCellNumbers) == true);
This is the code that I tried, numbers is an array of integers and I get this error:
'MemoryExtensions.Contains(ReadOnlySpan, int)' requires a receiver of type 'ReadOnlySpan'
I was attempting to skip all numbers that are in 'inCellNumbers' and have them in 'availableNumbers' from 'Numbers'
You can use the Enumerable.Except Method:
int[] availableNumbers = Numbers.Except(inCellNumbers).ToArray();
The Enumerable.SkipWhile Method stops skipping at the first element that does not fulfill the condition. Even if others follow later that do fulfill it.
I'm not sure of the algorithm for checking around a sudoku grid but it sounds like you would want to use some hashsets.
A hashset may only have unique entries. An array or list can be dumped to a hashset using its constructor new HashSet(arrOrList), and you can evaluate immediately to one from LINQ using .ToHashSet().
As for the error, it looks like you are using List<T>.Contains<T>(T item) (notice all of the T here), which is going to check if the List contains the entire parameterized array as a single element/item. Unless Numbers is of type List<int[]> this is going to be a problem, but I'm unsure of what type you give it so this may not be the true issue. I see you are using .Contains<int> but this won't work as inCellNumbers is int[]

Value of items in a c# listbox

I am sure if i look hard enough i can find my answer but so far i can't find a clear cut answer.
What i am trying to do is use the value of an item in a listbox which contains 7 items as a numerical identifier for a specific array element. (all items in the list are strings)
array[listbox.value] = my new data for that array element
i know i can pull the string of the item out but and that i can identify a specific item in the list with.
list1.Items[value].ToString();
i just want to know if i can do reverse the alternative is a pain to code as its a lot more lines of code comparing the string in the list to each item in my array until i find a match while i know all items in the list are the same order as the array.
Instead of using an array, you can use a dictionary.
Dictionary<string, valueType> myArray = new Dictionary<string, valueType>();
myArray["Item1"] = some value
myArray["Item2"] = some value
... etc
Then later
myArray[listbox.Value.ToString()] = my new value
Thats really the best way to refer to an array index by a string value. The type for the indexer does not necessarily need to be a string, it can be any type that is uniquely identifiable.

Start Index Of Multidimensional Array [duplicate]

Is it possible in c# to initialize an array in, for example, subindex 1?
I'm working with Office interop, and every property is an object array that starts in 1 (I assume it was originally programed in VB.NET), and you cannot modify it, you have to set the entire array for it to accept the changes.
As a workaround I am cloning the original array, modifying that one, and setting it as a whole when I'm done.
But, I was wondering if it was possible to create a new non-zero based array
It is possible to do as you request see the code below.
// Construct an array containing ints that has a length of 10 and a lower bound of 1
Array lowerBoundArray = Array.CreateInstance(typeof(int), new int[1] { 10 }, new int[1] { 1 });
// insert 1 into position 1
lowerBoundArray.SetValue(1, 1);
//insert 2 into position 2
lowerBoundArray.SetValue(2, 2);
// IndexOutOfRangeException the lower bound of the array
// is 1 and we are attempting to write into 0
lowerBoundArray.SetValue(1, 0);
You can use Array.CreateInstance.
See Array Types in .NET
Not simply. But you can certainly write your own class. It would have an array as a private variable, and the user would think his array starts at 1, but really it starts at zero and you're subtracting 1 from all of his array accesses.
You can write your own array class
I don't think if it's possible to modify the starting index of arrays.
I would create my own array using generics and handle it inside.
Just keep of const int named 'offset' with a value of one, and always add that to your subscripts in your code.
I don't think you can create non-zero based arrays in C#, but you could easily write a wrapper class of your own around the built in data structures.This wrapper class would hold a private instance of the array type you required; overloading the [] indexing operator is not allowed, but you can add an indexer to a class to make it behave like an indexable array, see here. The index function you write could then add (or subtract) 1, to all index's passed in.
You could then use your object as follows, and it would behave correctly:
myArrayObject[1]; //would return the zeroth element.
In VB6 you could change the array to start with 0 or 1, so I think VBScript can do the same. For C#, it's not possible but you can simply add NULL value in the first [0] and start real value at [1]. Of course, this is a little dangerous...

Convert ONE two-dimensional array into TWO one-dimensional arrays

You may feel weird about what I'm asking, but it's true, convert 1 two-dimensional array into 2 one-dimensional arrays.
That was what my teacher asked, and he said he would give a perfect point for whoever answers this (I think, tricky) question. I would happy to convert any 2 or n-dimensional array into one one-dimensional array. But he said 2, so I think there must be something to do with the second array. And, he didn't tell what type of array (int, String or object), so I assume it must be done with any kind of 2-dimensional array.
This is what I will answer him if no one here figure out what he wants: Convert into a 1-dimensional array, and leave the second null (or let it have no element). But I don't think it's a good answer for such a tricky question.
EDIT: Here is my teacher question, word-by-word (he just ask at the end of the session in voice, not in the textbook, as a bonus question(with... a nice bonus reward)): Given a 2-dimensional array, convert it into two 1-dimensional arrays.
I don't know if [][] in Java and C# considered 2-dimensional array, but C# does have [, ], which is 2-dimensional array. We are studying computer algorithm, with no target IDE or language.
EDIT2: I emailed him, and he refused to give additional information (he said it was unfair for others if I have more information than them), and he didn't give any comments about jagged array idea. The only useful thing in his reply: Let [][] be considered 2-dimensional array.
I'll bite. It's possible to flatten the entire two-dimensional array into the first of the two one-dimensional arrays by simply reading and writing consistently. I.e. store row 1 then row 2, etc. sequentially in that first array. Whenever you move to the next row, store the index of that next cell (of the first one dimensional array) in the second one-dimensional array, which would essentially become a row index table.
As Jon Skeet said above, this isn't a very well-specified question; perhaps with clarified information, we could better help you.
If I understand your question properly
it's easy m8...
it's only an algorithm question.. not a programming language specific...
you can do it like this:
one array holds the values
second array holds the keys
try to find a workaround in the second array to know what keys you've got..
For example:
array_1: v0 v1 v2 null v3 v4 v5 null v6 v7 v8 null
array_2: 0 1 2 newR 0 1 2 newR 0 1 2 newR
You can represent it in one array as well... but you need a specific algorithm to figure out when you are located on Y of the matrix.
The problem is that you won't access the data instantly from memory.. this is why there are bi-dimensional arrays
Another way:
keep in array 1 the values
keep in the second array the keys as string like in the following example:
array1: value1 value2 value3 value4 value5
array2: 0,0 0,1 1,0 1,1 2,0
there are a lot of algorithms but I don't think you will find better than bi-dimensional arrays...
When you look after them you will have less performance.. ofc.. unless you keep them in hashtables.. hashing 0,0 and added as key in a hashtable and add the specified value to that key. then you will look for key "0,0"...
Flatten the 2-d array in either row-major or column-major order, storing it in one of the 1-d arrays. Store the shape {n, m} of the array in the other 1-d integer array. Given the indices for an element in the 2-d array of values, you can use the shape to calculate the index in the 1-d array of values.
The two representations are isomorphic, and both allow looking up the values in constant time. It's also similar to how a 2-d array is represented in memory.
I guess you want to transform you 2-demension array (typed RelevantType[,]) into 2 arrays (typed SomeTypeA[] and SomeTypeB[]) without losing any information?
It's not very difficult:
Have the first array be of type RelevantType[], the second one of type int[], copy the content of your 2-dimensionnal array into the first one and its first indices into the second one, and you're done.

any dynamic length two dimension structure in C#?

Please guide me, are there any out of box data structures in C# that support dynamic length and two or more dimensions?
I tried to use a string array but length is an issue. I want some structure that can be built and support 2 dimensions, like some text and its value.
Please suggest solutions.
How about a List<List<T>>?
First of all, "two or more dimensions" isn't all that specific.
Do you need 2? Or 3? Or more? What is "more" anyway?
Also, do you need a rectangular data set like a 2 dimensional array? Or do you need one that has different widths for different rows?
Also, your "some text and its value" suggests you're not really looking at a 2 dimensional data structure, but a 1 dimensional data structure, containing elements that have multiple values.
The difference here is that a 2 dimensional data structure would have the same type of elements in both dimensions, whereas the other can have different types of elements, like an int and a string.
So perhaps you need to tell us a bit more about what you need to store in that structure.
Otherwise, List<List<T>> it is.
If you want key-value pairs you can use a Hashtable or NameValueCollection.
It's not clear what you're asking. There are many ways to use the generic collections to build 2-dimensional data structures. What is it that you really want?
If you're talking about text data that you want to split into lines, and those lines can be modified, then how about List<StringBuilder>? That will allow you to add and delete lines, and you can also modify the individual lines.

Categories