简体   繁体   中英

What is the fastest way to make objectIds unique in an Array?

What is the fastest way to make objectIds unique in an Array? For testing if 2 objectIds are equal, may I convert objectIds to other type, such as numbers or strings?

The easiest way would be to add the ids as keys of an object like this:

var array = [1, 2, 3, 4, 5, 6, 2, 3, 4]
var obj = {}
array.forEach(function(id){obj[id] = true})
array = Object.keys(obj)

Other alternatives would be sorting the array and using binary search and using some data structure like AVL tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM