Question about HashSet<int[]>

Elad

Member
Joined
Feb 15, 2020
Messages
20
Programming Experience
1-3
Hey!
I wanted to find out, is it true to use an entire HashSet array?
Or the HashSet object is for certain real things, and not for any array!
However, if there is no logical limit whatsoever
I wanted to find out how I could anyway
HashSet does not need to replicate a repeatable array, I know that in classes inherit the IEquatable interface and change the Equals and GetHashCode functions
But an array of int variables (though inherited from the int32 structure) is a little more complicated by comparison, I've tried all sorts of built-in function techniques already from the HashSet object itself, but for some reason it doesn't help
More than that I can say that I built a class with a whole array property, inherited the IEquatable interface and changed the Equal and GetHashCode functions (to match the class content) but it didn't work!
The problem is more complex, when I create a one-dimensional whole array temporarily and I keep random numbers through the random class built into this array and then add it to HashSet, which I noticed (in Debuger) not only does it duplicate every other array to the same HashSet object Actually the number of times I really wanted to keep random numbers in the array!
This phenomenon reminds me, basically, of the random function built in C language that is not only used but also really adds a Time library and another function of span random, exclusive to the time library and the extra random function: random range without changing!
If anyone has an explanation, everything I mentioned above would love to hear!
 
Please show us a minimal example of the code that is not working, and tell us what you were expecting to see.

The problem is more complex, when I create a one-dimensional whole array temporarily and I keep random numbers through the random class built into this array and then add it to HashSet, which I noticed (in Debuger) not only does it duplicate every other array to the same HashSet object
This suggests to me that you have lost track of your references. I'm guessing you are doing something like this:
C#:
var hashSet = new HashSet<int []>();
var random = new Random();
var arr = new int[IntegersToAddCount];
for(int i = 0; i < ArraysToAddCount; i++)
{
    for(int j = 0; j < arr.Length; j++)
        arr[j] = random.Next();
    hashSet.Add(arr);
}

Notice the bug above where you are re-using the same instance of the array.

The correct thing to do is to always create a new instance of the array:
C#:
var hashSet = new HashSet<int []>();
var random = new Random();
for(int i = 0; i < ArraysToAddCount; i++)
{
    var arr = new int[IntegersToAddCount];
    for(int j = 0; j < arr.Length; j++)
        arr[j] = random.Next();
    hashSet.Add(arr);
}
 
Skydiver

Thank you! The code works well without problems now
Thanks to you!
Here is my code snippet
If you have a tip to make the code more effective I'd love to hear

C#:
using System.IO;
using System.Linq;
using System.Text;

namespace HashSetOfArrayInt
{
    class Program
    {
        static HashSet<int[]> hashNum = new HashSet<int[]>();
        const int SIZE = 900000;

        static void Main(string[] args)
        {
            AddPattrenOfNum();
        }

        static void AddPattrenOfNum()
        {
            var rand = new Random();

            for (int i = 0; i < SIZE; i++)
            {
                var arrTemp = new int[8];

                for (int j = 0; j < arrTemp.Length-1)
                {
                    int num = rand.Next(1, 387);
                    if(!arrTemp.Contains(num))
                    {
                        arrTemp[j] = num;
                        j++;
                    }
                }

                Array.Sort(arrTemp);
                arrTemp[0] = rand.Next(1, 210);

                hashNum .Add(arrTemp);
            }
        }
    }
}
 
Last edited by a moderator:
So in this loop, you are trying to make sure that there are unique numbers:
C#:
for (int j = 0; j < arrTemp.Length-1)
{
    int num = rand.Next(1, 387);
    if(!arrTemp.Contains(num))
    {
        arrTemp[j] = num;
        j++;
    }
}
but then when you go replace the first element of the array after sorting:
C#:
arrTemp[0] = rand.Next(1, 210);
You don't do anything to make sure that number is unique.

My suggestion is stick in a value into the first element first, and then loop to fill in the rest of the elements with unique values.
 
Here is my code snippet
Please use code tags when posting to the forums. Thanks

nD918n6.gif
 
I forgot to mention what I do
So I'll explain,
This is a program that produces 8 numbers and a winning number determined by the first index.

You are right that the purpose of the program is not the most logical, but the part is to learn to use with HashSet even with different types. The idea of the program is that HashSet will contain a series of numbered numbers in one row and if the number in the first index is found again in the same set of 7 numbers the same number as "Won"!
For example, suppose the numbers are randomly drawn: 45,73,23,107,33,278,91 and after sorting we added a random number to the first index (from 1 to 387 and not as specified in code to 210) which is 107 and then the number 107 won. If there is no number at all in the array then there is no winning number!

And my real problem was that in HashSet, in fact, 8 numbers were saved that they themselves appeared again in the object itself!
 
Ah. Okay.
 
Wait. That still doesn't make sense. So you generate 8 unique numbers. Then you discard the lowest value of the 8 numbers and replace it with a random value between 1 and 210. What if the lowest value was the winning number, but you just overwrote it?
 
Also, it is not clear what a HashSet is buying you here. You would just as easily use a List since the array of integers is not going to be able to tell the hash set if there is a collision or not.
 
Back
Top Bottom