Should I use Array or Set if both can be used to finish my task?
Clash Royale CLAN TAG#URR8PPP
up vote
2
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?
javascript coding-style array
add a comment |Â
up vote
2
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?
javascript coding-style array
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
Oct 4 at 6:56
How are you going to be usingresults
? 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 (ormap
ing) over each value?
â Caleth
Oct 4 at 8:17
My own rule of thumb, always List if I return something supposed to be only read even if they're unique because a set is way heavier (time, memory) to build (at least an HastSet and even an identityset will be heavier than a default List based on simple Array implementation). So I use only Set when I'm sure that not only my results are unique, but also that this collection may be written and I need to ensure the uniqueness. An example : when transforming a list to a map of <key,list<value>> in the old school way, I use a set for the keys.
â Walfrat
Oct 4 at 11:55
add a comment |Â
up vote
2
down vote
favorite
up vote
2
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?
javascript coding-style array
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
javascript coding-style array
asked Oct 4 at 5:59
mmmaaa
2,09031319
2,09031319
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
Oct 4 at 6:56
How are you going to be usingresults
? 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 (ormap
ing) over each value?
â Caleth
Oct 4 at 8:17
My own rule of thumb, always List if I return something supposed to be only read even if they're unique because a set is way heavier (time, memory) to build (at least an HastSet and even an identityset will be heavier than a default List based on simple Array implementation). So I use only Set when I'm sure that not only my results are unique, but also that this collection may be written and I need to ensure the uniqueness. An example : when transforming a list to a map of <key,list<value>> in the old school way, I use a set for the keys.
â Walfrat
Oct 4 at 11:55
add a comment |Â
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
Oct 4 at 6:56
How are you going to be usingresults
? 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 (ormap
ing) over each value?
â Caleth
Oct 4 at 8:17
My own rule of thumb, always List if I return something supposed to be only read even if they're unique because a set is way heavier (time, memory) to build (at least an HastSet and even an identityset will be heavier than a default List based on simple Array implementation). So I use only Set when I'm sure that not only my results are unique, but also that this collection may be written and I need to ensure the uniqueness. An example : when transforming a list to a map of <key,list<value>> in the old school way, I use a set for the keys.
â Walfrat
Oct 4 at 11:55
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
Oct 4 at 6:56
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
Oct 4 at 6:56
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 map
ing) over each value?â Caleth
Oct 4 at 8:17
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 map
ing) over each value?â Caleth
Oct 4 at 8:17
My own rule of thumb, always List if I return something supposed to be only read even if they're unique because a set is way heavier (time, memory) to build (at least an HastSet and even an identityset will be heavier than a default List based on simple Array implementation). So I use only Set when I'm sure that not only my results are unique, but also that this collection may be written and I need to ensure the uniqueness. An example : when transforming a list to a map of <key,list<value>> in the old school way, I use a set for the keys.
â Walfrat
Oct 4 at 11:55
My own rule of thumb, always List if I return something supposed to be only read even if they're unique because a set is way heavier (time, memory) to build (at least an HastSet and even an identityset will be heavier than a default List based on simple Array implementation). So I use only Set when I'm sure that not only my results are unique, but also that this collection may be written and I need to ensure the uniqueness. An example : when transforming a list to a map of <key,list<value>> in the old school way, I use a set for the keys.
â Walfrat
Oct 4 at 11:55
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
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:
What does the consumer need to do with the container?
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. Apush()
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, whereArray.push()
is happy to push an element twice,Set.insert()
must somehow catch this condition and handle it. This requires extra work. To getO(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 thanArray.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.
add a comment |Â
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.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
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:
What does the consumer need to do with the container?
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. Apush()
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, whereArray.push()
is happy to push an element twice,Set.insert()
must somehow catch this condition and handle it. This requires extra work. To getO(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 thanArray.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.
add a comment |Â
up vote
6
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:
What does the consumer need to do with the container?
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. Apush()
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, whereArray.push()
is happy to push an element twice,Set.insert()
must somehow catch this condition and handle it. This requires extra work. To getO(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 thanArray.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.
add a comment |Â
up vote
6
down vote
up vote
6
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:
What does the consumer need to do with the container?
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. Apush()
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, whereArray.push()
is happy to push an element twice,Set.insert()
must somehow catch this condition and handle it. This requires extra work. To getO(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 thanArray.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.
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:
What does the consumer need to do with the container?
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. Apush()
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, whereArray.push()
is happy to push an element twice,Set.insert()
must somehow catch this condition and handle it. This requires extra work. To getO(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 thanArray.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.
answered Oct 4 at 9:18
cmaster
5,53511118
5,53511118
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Oct 4 at 10:09
answered Oct 4 at 7:30
Doc Brown
126k21230366
126k21230366
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Oct 4 at 6:56
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 (ormap
ing) over each value?â Caleth
Oct 4 at 8:17
My own rule of thumb, always List if I return something supposed to be only read even if they're unique because a set is way heavier (time, memory) to build (at least an HastSet and even an identityset will be heavier than a default List based on simple Array implementation). So I use only Set when I'm sure that not only my results are unique, but also that this collection may be written and I need to ensure the uniqueness. An example : when transforming a list to a map of <key,list<value>> in the old school way, I use a set for the keys.
â Walfrat
Oct 4 at 11:55