Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add elements to int hashset from int array

It is possible to iterate over array and then add elements to hashset one by one. Is there any way to add integers from int array to int hashset without iterating over array elements??

 int[] pagesid;//int array
 var deletepages = new HashSet<int>();//hashset 
 pagesid= Array.ConvertAll(text.Split(','), s=>int.Parse(s));
 //values from pagesid should be added to hashset.

Update:

int[] pagesid;//int array
var deletepages = new HashSet<int>();//hashset 
foreach (XmlNode rule in pgmgmtrules)
{
  ruleresult=doc.ParseText(rule.InnerText, false);//parse rule
  if (ruleresult != "")
  { //if parsed rule result has value
    if (rule.Attributes["Action"].Value == "Delete")
    {
      var text=rule.Attributes["pageids"].Value;                                     
      pagesid= Array.ConvertAll(text.Split(','), s=>int.Parse(s));
     //add elements from pagesid array to hashset

    }
  }
}
like image 630
Milind Anantwar Avatar asked Mar 21 '26 04:03

Milind Anantwar


2 Answers

You can use overloaded constructor HashSet Constructor (IEnumerable) which takes IEnumerable<T> as parameter instead of using the default constructor HashSet<T>().

HashSet<int> evenNumbers = new HashSet<int>(text.Split(',').Select(int.Parse));

HashSet Constructor (IEnumerable)

Initializes a new instance of the HashSet class that uses the default equality comparer for the set type, contains elements copied from the specified collection, and has sufficient capacity to accommodate the number of elements copied.

Edit 1 If you want to filter even number from the array then you can use Where

var intArr = text.Split(',').Select(int.Parse);
HashSet<int> evenNumbers = new HashSet<int>(intArr.Where(i=>i%2==0));

Edit 2 Based on comments. You can use List<int> instead of int array. Keeping adding the int pagesids in List in the loop and when loop is finished add the List in HashSet through its constructor HashSet Constructor (IEnumerable) .

List<int> pagesid = new List<int>();//int array
HashSet<int> deletepages = null;
foreach (XmlNode rule in pgmgmtrules)
{
  ruleresult=doc.ParseText(rule.InnerText, false);//parse rule
  if (ruleresult != "")
  { //if parsed rule result has value
    if (rule.Attributes["Action"].Value == "Delete")
    {
      var text=rule.Attributes["pageids"].Value;                                     
      pagesid.AddRange(text.Split(',').Select(int.Parse));
    }
  }
}
//add elements from pagesid array to hashset
deletepages =  new HashSet<int>(pagesid);
like image 198
Adil Avatar answered Mar 22 '26 21:03

Adil


try this simple way from your int array

 int[] pagesid;//int array
 var deletepages = new HashSet<int>();//hashset 
 pagesid = Array.ConvertAll("3,5,6,7".Split(','), s => int.Parse(s));
//values from pagesid should be added to hashset.
 var hashset = new HashSet<int>(pagesid);
like image 29
faby Avatar answered Mar 22 '26 20:03

faby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!