/    Sign up×
Community /Pin to ProfileBookmark

simple JavaScript array-related questions

Hello

I am looking for an efficient way to write the followng pseudo-code:

1) I start with an empty hashtable
2) I iterate over an array holding various integers (for example: 2, 100, 5555, 234)
3) For each integer, I need to check if it is already in the hashtable
4) If not, then I need to add it to the hashtable
5) If yes, I need to add it to a second array that will hold all the repeating integers

My questions are:
– How can I define a hashtable in JavaScript? Can I use arrays? I want them to be as efficient as hashtable in terms of element lookup. Step 3 will take O(N) in an array but O(1) in hashtable
– Do the keys have to be strings, or can I use integers and still enjoy a hashtable
– How can I tell if an element is already in the hashtable (array) ?

regards

to post a comment
JavaScript

1 Comments(s)

×

Success!

Help @jasongr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...