Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1085241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:39:54+00:00 2026-05-16T22:39:54+00:00

I am a bit at a loss how to do the following efficiently in

  • 0

I am a bit at a loss how to do the following efficiently in Mathematica:

a = { 1, 2, 3, 4, 5 };  (* list of integers *)
b = { 2, 4, 6, 8 };     (* another list of integers *)
filter = Table[MemberQ[b, element], {element,a}]

Expected output is:

{False, True, False, True, False}

My lists a and b are big, so Mathematica is doing a kazillion linear searches through b. I want it to do faster lookups with a hashtable. But there seems to be no such structure. The closest I could find is a SparseArray, but

sa = SparseArray[{1 -> True, 2 -> True}];
MemberQ[sa, 1]

is False.

I’m sure this must be possible in Mathematica in one line of code or less, I just can’t see it for the trees, or something.

Any hero to the rescue? Meanwhile, I’m going to do this with C#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:39:54+00:00Added an answer on May 16, 2026 at 10:39 pm

    Another idea would be to use rules and Dispatch. It seems to be faster when the length of the blist is large:

    alist = Prime /@ Range[20000];
    blist = alist + 2;
    
    membQ = First@Timing[MemberQ[blist,#]&/@alist;]
    
    sparse = First@Timing[
        s = SparseArray[blist -> ConstantArray[True, Length@blist], Max[alist, blist], False];
        s[[#]] & /@ alist;
    ]
    
    rules = First@Timing[
        bRules = Dispatch[Append[Rule[#, True] & /@ blist, _ -> False]];
        (# /. bRules) & /@ alist;
    ]
    
    fct = First@Timing[
        f[_] = False;
        (f[#] = True) & /@ blist;
        f /@ alist;
    ]
    

    On my laptop, rules (0.06s) < fct (0.09s) < sparse (0.9s) < membQ (40s).

    Edit / Comparing fct and rules timings

    @Karsten please feel free to rollback to your original answer

    Tables generated with Prime[…]

    alt text

    Tables generated with RandomInteger[…]

    alt text

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit at a loss as to how to find a clean algorithm
I'm at a bit of a loss as to why one of my ASP
Not much code since I'm a bit at a loss on how to start.
What does the ISupportErrorInfo interface mean? I'm at a bit of a loss to
I'm at a bit of a loss here. I'm creating a collection of objects
the following bit of code is run regularly as a cronjob and is turning
I'm at a bit of a loss with this one, I have a nine
In the following code I have an error possible loss of precision found :
Running into a bit of a conundrum. The following image is a site title
I seem to be a bit at loss with the status and development of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.