最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pop random element from a set - JS - Stack Overflow

programmeradmin0浏览0评论

In e.g. Python if we have a set we can call set.pop() and it will remove a value from the set and return it.

I'm working through an algorithms question in JS where I need to do this. But there doesn't seem to be a way to, as the set.delete() function expects a value, and I don't have access to the values. Is there an easy work around?

In e.g. Python if we have a set we can call set.pop() and it will remove a value from the set and return it.

I'm working through an algorithms question in JS where I need to do this. But there doesn't seem to be a way to, as the set.delete() function expects a value, and I don't have access to the values. Is there an easy work around?

Share Improve this question asked Apr 22, 2022 at 19:18 Wilson Brians Wilson Brians 1773 silver badges9 bronze badges 4
  • @vsync agreed on being XY, not sure what you propose is the X. Might just be "how I get any value from a Set" without having to be a random every time. The first (or last) might also be sufficient. OP should clarify what the real requirement is. – VLAZ Commented Apr 22, 2022 at 19:21
  • 1 Please show us the code because I don't really understand the question 100% – vsync Commented Apr 22, 2022 at 19:22
  • Put all the possible values in an array and randomize a number, then access the array at that index. And there you have it - the value which should now be randomly removed. use that value when calling delete() – vsync Commented Apr 22, 2022 at 19:24
  • @vsync "just put all the possible values in an array and randomize a number and there you have it - the value which should now be randomly removed. use that value when calling delete()" - yeah, I guess my question was whether there was a simpler way to do this in JS. But I guess not. TY though – Wilson Brians Commented Apr 22, 2022 at 19:28
Add a ment  | 

3 Answers 3

Reset to default 4

If saying "random" you mean any element, then this is a solution:

function shiftSet(set) {
  for (const value of set) {
    set.delete(value)
    return value
  }
}

const set = new Set(['a', 'b', 'c'])
shiftSet(set) // 'a'
set // {'b', 'c'}

It removes the earliest element added to the stack and returns it.

You can iterate over the values. (It would be faster than converting it to an array.)

function deleteRandomElement(set) {
  if (set.size > 0) {
      const randomIndex = Math.floor(Math.random()*set.size);
      let i = 0;
      
      for(const value of set) {
        if(i === randomIndex) {
          set.delete(value);
          break;
        }
        i++;
      }
  }
}

If I understand your question correctly, you want to delete an element from a set in Javascript and return the removed value, like this:

const set = new Set([1, 2, 3, 4, 5))
const elem = set.delete()

You expect elem to be any of 1, 2, 3, 4, 5. But the delete() method in Javascript set only returns true or false and there is no other method like pop() in Python.

Here's how to do it:

  • Call values() on the set instance to get a set iterator object that contains all the elements of the set in insertion order.
  • To access the next value in the iteration sequence, call next() on this iterator object, which will return an object with property value. This is the next value that we're looking for.
const iterator = set.values()
const nextVal = iterator.next().value
console.log(nextVal) // expect 1
发布评论

评论列表(0)

  1. 暂无评论