Should I use Array or Set if both can be used to finish my task?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












for example, Suppose I have a 2d array:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];


I want to find and store the index of column that all are 1, i.e.:position 1 and 3, and the codes are the following:



Use Array:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];

let results=;
for(let i=0;i<filterArr[0].length;i++)
let isAllOne=true;
for(let filter of filterArr)
if(filter[i]!=1)
isAllOne=false;
break;


if(isAllOne)
results.push(i);


//store results for other use


Use Set:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];

let results=new Set();
for(let i=0;i<filterArr[0].length;i++)
let isAllOne=true;
for(let filter of filterArr)
if(filter[i]!=1)
isAllOne=false;
break;


if(isAllOne)
results.insert(i);


//store results for other use


Which I found "results" can be either Array or Set. My question is, not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set?










share|improve this question

















  • 2




    Both Array and Set has equivalent complexity for insert operations which is O(1): constant. So it depends on what you are planning to do with the result collection...
    – Arcones
    3 hours ago











  • How are you going to be using results? Are you going to do set-like things, such as checking if a value is an element of that set, or are you only going to do collection like things, such as looping (or maping) over each value?
    – Caleth
    2 hours ago
















up vote
1
down vote

favorite












for example, Suppose I have a 2d array:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];


I want to find and store the index of column that all are 1, i.e.:position 1 and 3, and the codes are the following:



Use Array:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];

let results=;
for(let i=0;i<filterArr[0].length;i++)
let isAllOne=true;
for(let filter of filterArr)
if(filter[i]!=1)
isAllOne=false;
break;


if(isAllOne)
results.push(i);


//store results for other use


Use Set:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];

let results=new Set();
for(let i=0;i<filterArr[0].length;i++)
let isAllOne=true;
for(let filter of filterArr)
if(filter[i]!=1)
isAllOne=false;
break;


if(isAllOne)
results.insert(i);


//store results for other use


Which I found "results" can be either Array or Set. My question is, not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set?










share|improve this question

















  • 2




    Both Array and Set has equivalent complexity for insert operations which is O(1): constant. So it depends on what you are planning to do with the result collection...
    – Arcones
    3 hours ago











  • How are you going to be using results? Are you going to do set-like things, such as checking if a value is an element of that set, or are you only going to do collection like things, such as looping (or maping) over each value?
    – Caleth
    2 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











for example, Suppose I have a 2d array:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];


I want to find and store the index of column that all are 1, i.e.:position 1 and 3, and the codes are the following:



Use Array:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];

let results=;
for(let i=0;i<filterArr[0].length;i++)
let isAllOne=true;
for(let filter of filterArr)
if(filter[i]!=1)
isAllOne=false;
break;


if(isAllOne)
results.push(i);


//store results for other use


Use Set:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];

let results=new Set();
for(let i=0;i<filterArr[0].length;i++)
let isAllOne=true;
for(let filter of filterArr)
if(filter[i]!=1)
isAllOne=false;
break;


if(isAllOne)
results.insert(i);


//store results for other use


Which I found "results" can be either Array or Set. My question is, not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set?










share|improve this question













for example, Suppose I have a 2d array:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];


I want to find and store the index of column that all are 1, i.e.:position 1 and 3, and the codes are the following:



Use Array:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];

let results=;
for(let i=0;i<filterArr[0].length;i++)
let isAllOne=true;
for(let filter of filterArr)
if(filter[i]!=1)
isAllOne=false;
break;


if(isAllOne)
results.push(i);


//store results for other use


Use Set:



let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];

let results=new Set();
for(let i=0;i<filterArr[0].length;i++)
let isAllOne=true;
for(let filter of filterArr)
if(filter[i]!=1)
isAllOne=false;
break;


if(isAllOne)
results.insert(i);


//store results for other use


Which I found "results" can be either Array or Set. My question is, not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set?







javascript coding-style array






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









mmmaaa

2,08531319




2,08531319







  • 2




    Both Array and Set has equivalent complexity for insert operations which is O(1): constant. So it depends on what you are planning to do with the result collection...
    – Arcones
    3 hours ago











  • How are you going to be using results? Are you going to do set-like things, such as checking if a value is an element of that set, or are you only going to do collection like things, such as looping (or maping) over each value?
    – Caleth
    2 hours ago












  • 2




    Both Array and Set has equivalent complexity for insert operations which is O(1): constant. So it depends on what you are planning to do with the result collection...
    – Arcones
    3 hours ago











  • How are you going to be using results? Are you going to do set-like things, such as checking if a value is an element of that set, or are you only going to do collection like things, such as looping (or maping) over each value?
    – Caleth
    2 hours ago







2




2




Both Array and Set has equivalent complexity for insert operations which is O(1): constant. So it depends on what you are planning to do with the result collection...
– Arcones
3 hours ago





Both Array and Set has equivalent complexity for insert operations which is O(1): constant. So it depends on what you are planning to do with the result collection...
– Arcones
3 hours ago













How are you going to be using results? Are you going to do set-like things, such as checking if a value is an element of that set, or are you only going to do collection like things, such as looping (or maping) over each value?
– Caleth
2 hours ago




How are you going to be using results? Are you going to do set-like things, such as checking if a value is an element of that set, or are you only going to do collection like things, such as looping (or maping) over each value?
– Caleth
2 hours ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote













The use of either Array or Set by your function is equivalent: You create a container, and you push stuff into it. This leaves two areas for consideration:



  1. What does the consumer need to do with the container?


  2. Which container is more efficient (space or time)?


If you find that the consumer just iterates over the container, both Array and Set are equivalent again from this usage perspective. However, if your user needs to answer the question "is this index in the container?", then Set is the only appropriate container type.



Once you have asserted that the container type is indeed irrelevant to use, both on the producer and consumer side, go for the more efficient container:



  • Array should be implemented as a simple slap of contiguous memory that holds (the references to) its elements. A push() generally means 1) checking for space (one comparison), 2) appending the new element to the end of the buffer, and 3) updating the length field.


  • Set is required to ensure that its elements are unique. That is, where Array.push() is happy to push an element twice, Set.insert() must somehow catch this condition and handle it. This requires extra work. To get O(1) performance, Set would normally be implemented as a hash table. Hash tables are fast, but they are definitely more complex than a simple, contiguous buffer. Thus, Set.insert() will generally have to do significantly more work than Array.push(), and its data structures are very likely to consume more memory.


Both space and time efficiency are better for Array than for Set. And this is why I would default to using Array over Set whenever their use is equivalent.






share|improve this answer



























    up vote
    2
    down vote













    From what you have shown in your question, it does not matter (and consider to refactor your code in case it starts to matter).




    not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set




    Producing an Array or a Set is typically not an end in itself, it is a means to an end, so often you can find out what the caller of your method which produces this result might prefer.



    Note, if you start using an array, and it turns out for further processing a Set would be beneficial (or you need both, the array and the set representation), you can easily convert it:



     let resultSet = new Set(results)


    I am not a Javascript expert, but I would expect the conversion the other way round to require at a little bit more code. Thus, if in doubt, I personally would start with an array first, and switch to a set if it fits better, but that is somewhat opinionated.






    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "131"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f379419%2fshould-i-use-array-or-set-if-both-can-be-used-to-finish-my-task%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      The use of either Array or Set by your function is equivalent: You create a container, and you push stuff into it. This leaves two areas for consideration:



      1. What does the consumer need to do with the container?


      2. Which container is more efficient (space or time)?


      If you find that the consumer just iterates over the container, both Array and Set are equivalent again from this usage perspective. However, if your user needs to answer the question "is this index in the container?", then Set is the only appropriate container type.



      Once you have asserted that the container type is indeed irrelevant to use, both on the producer and consumer side, go for the more efficient container:



      • Array should be implemented as a simple slap of contiguous memory that holds (the references to) its elements. A push() generally means 1) checking for space (one comparison), 2) appending the new element to the end of the buffer, and 3) updating the length field.


      • Set is required to ensure that its elements are unique. That is, where Array.push() is happy to push an element twice, Set.insert() must somehow catch this condition and handle it. This requires extra work. To get O(1) performance, Set would normally be implemented as a hash table. Hash tables are fast, but they are definitely more complex than a simple, contiguous buffer. Thus, Set.insert() will generally have to do significantly more work than Array.push(), and its data structures are very likely to consume more memory.


      Both space and time efficiency are better for Array than for Set. And this is why I would default to using Array over Set whenever their use is equivalent.






      share|improve this answer
























        up vote
        3
        down vote













        The use of either Array or Set by your function is equivalent: You create a container, and you push stuff into it. This leaves two areas for consideration:



        1. What does the consumer need to do with the container?


        2. Which container is more efficient (space or time)?


        If you find that the consumer just iterates over the container, both Array and Set are equivalent again from this usage perspective. However, if your user needs to answer the question "is this index in the container?", then Set is the only appropriate container type.



        Once you have asserted that the container type is indeed irrelevant to use, both on the producer and consumer side, go for the more efficient container:



        • Array should be implemented as a simple slap of contiguous memory that holds (the references to) its elements. A push() generally means 1) checking for space (one comparison), 2) appending the new element to the end of the buffer, and 3) updating the length field.


        • Set is required to ensure that its elements are unique. That is, where Array.push() is happy to push an element twice, Set.insert() must somehow catch this condition and handle it. This requires extra work. To get O(1) performance, Set would normally be implemented as a hash table. Hash tables are fast, but they are definitely more complex than a simple, contiguous buffer. Thus, Set.insert() will generally have to do significantly more work than Array.push(), and its data structures are very likely to consume more memory.


        Both space and time efficiency are better for Array than for Set. And this is why I would default to using Array over Set whenever their use is equivalent.






        share|improve this answer






















          up vote
          3
          down vote










          up vote
          3
          down vote









          The use of either Array or Set by your function is equivalent: You create a container, and you push stuff into it. This leaves two areas for consideration:



          1. What does the consumer need to do with the container?


          2. Which container is more efficient (space or time)?


          If you find that the consumer just iterates over the container, both Array and Set are equivalent again from this usage perspective. However, if your user needs to answer the question "is this index in the container?", then Set is the only appropriate container type.



          Once you have asserted that the container type is indeed irrelevant to use, both on the producer and consumer side, go for the more efficient container:



          • Array should be implemented as a simple slap of contiguous memory that holds (the references to) its elements. A push() generally means 1) checking for space (one comparison), 2) appending the new element to the end of the buffer, and 3) updating the length field.


          • Set is required to ensure that its elements are unique. That is, where Array.push() is happy to push an element twice, Set.insert() must somehow catch this condition and handle it. This requires extra work. To get O(1) performance, Set would normally be implemented as a hash table. Hash tables are fast, but they are definitely more complex than a simple, contiguous buffer. Thus, Set.insert() will generally have to do significantly more work than Array.push(), and its data structures are very likely to consume more memory.


          Both space and time efficiency are better for Array than for Set. And this is why I would default to using Array over Set whenever their use is equivalent.






          share|improve this answer












          The use of either Array or Set by your function is equivalent: You create a container, and you push stuff into it. This leaves two areas for consideration:



          1. What does the consumer need to do with the container?


          2. Which container is more efficient (space or time)?


          If you find that the consumer just iterates over the container, both Array and Set are equivalent again from this usage perspective. However, if your user needs to answer the question "is this index in the container?", then Set is the only appropriate container type.



          Once you have asserted that the container type is indeed irrelevant to use, both on the producer and consumer side, go for the more efficient container:



          • Array should be implemented as a simple slap of contiguous memory that holds (the references to) its elements. A push() generally means 1) checking for space (one comparison), 2) appending the new element to the end of the buffer, and 3) updating the length field.


          • Set is required to ensure that its elements are unique. That is, where Array.push() is happy to push an element twice, Set.insert() must somehow catch this condition and handle it. This requires extra work. To get O(1) performance, Set would normally be implemented as a hash table. Hash tables are fast, but they are definitely more complex than a simple, contiguous buffer. Thus, Set.insert() will generally have to do significantly more work than Array.push(), and its data structures are very likely to consume more memory.


          Both space and time efficiency are better for Array than for Set. And this is why I would default to using Array over Set whenever their use is equivalent.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          cmaster

          5,50511118




          5,50511118






















              up vote
              2
              down vote













              From what you have shown in your question, it does not matter (and consider to refactor your code in case it starts to matter).




              not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set




              Producing an Array or a Set is typically not an end in itself, it is a means to an end, so often you can find out what the caller of your method which produces this result might prefer.



              Note, if you start using an array, and it turns out for further processing a Set would be beneficial (or you need both, the array and the set representation), you can easily convert it:



               let resultSet = new Set(results)


              I am not a Javascript expert, but I would expect the conversion the other way round to require at a little bit more code. Thus, if in doubt, I personally would start with an array first, and switch to a set if it fits better, but that is somewhat opinionated.






              share|improve this answer


























                up vote
                2
                down vote













                From what you have shown in your question, it does not matter (and consider to refactor your code in case it starts to matter).




                not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set




                Producing an Array or a Set is typically not an end in itself, it is a means to an end, so often you can find out what the caller of your method which produces this result might prefer.



                Note, if you start using an array, and it turns out for further processing a Set would be beneficial (or you need both, the array and the set representation), you can easily convert it:



                 let resultSet = new Set(results)


                I am not a Javascript expert, but I would expect the conversion the other way round to require at a little bit more code. Thus, if in doubt, I personally would start with an array first, and switch to a set if it fits better, but that is somewhat opinionated.






                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  From what you have shown in your question, it does not matter (and consider to refactor your code in case it starts to matter).




                  not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set




                  Producing an Array or a Set is typically not an end in itself, it is a means to an end, so often you can find out what the caller of your method which produces this result might prefer.



                  Note, if you start using an array, and it turns out for further processing a Set would be beneficial (or you need both, the array and the set representation), you can easily convert it:



                   let resultSet = new Set(results)


                  I am not a Javascript expert, but I would expect the conversion the other way round to require at a little bit more code. Thus, if in doubt, I personally would start with an array first, and switch to a set if it fits better, but that is somewhat opinionated.






                  share|improve this answer














                  From what you have shown in your question, it does not matter (and consider to refactor your code in case it starts to matter).




                  not limited to this case, if I found both Array and Set can finish my task, should I use Array or Set




                  Producing an Array or a Set is typically not an end in itself, it is a means to an end, so often you can find out what the caller of your method which produces this result might prefer.



                  Note, if you start using an array, and it turns out for further processing a Set would be beneficial (or you need both, the array and the set representation), you can easily convert it:



                   let resultSet = new Set(results)


                  I am not a Javascript expert, but I would expect the conversion the other way round to require at a little bit more code. Thus, if in doubt, I personally would start with an array first, and switch to a set if it fits better, but that is somewhat opinionated.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 16 mins ago

























                  answered 2 hours ago









                  Doc Brown

                  126k21230366




                  126k21230366



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f379419%2fshould-i-use-array-or-set-if-both-can-be-used-to-finish-my-task%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      Installing NextGIS Connect into QGIS 3?

                      One-line joke