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 373559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:19:12+00:00 2026-05-12T14:19:12+00:00

I have a sorted list of inputs: let x = [2; 4; 6; 8;

  • 0

I have a sorted list of inputs:

let x = [2; 4; 6; 8; 8; 10; 12]
let y = [-8; -7; 2; 2; 3; 4; 4; 8; 8; 8;]

I want to write a function which behaves similar to an SQL INNER JOIN. In other words, I want to return the cartesian product of x and y which contains only items shared in both lists:

join(x, y) = [2; 2; 4; 4; 8; 8; 8; 8; 8; 8]

I’ve written a naive version as follows:

let join x y =
    [for x' in x do
        for y' in y do
            yield (x', y')]
    |> List.choose (fun (x, y) -> if x = y then Some x else None)

It works, but this runs in O(x.length * y.length). Since both my lists are sorted, I think its possible to get the results I want in O(min(x.length, y.length)).

How can I find common elements in two sorted lists in linear time?

  • 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-12T14:19:12+00:00Added an answer on May 12, 2026 at 2:19 pm

    O(min(n,m)) time is impossible: Take two lists [x;x;…;x;y] and [x;x;…;x;z]. You have to browse both lists till the end to compare y and z.

    Even O(n+m) is impossible. Take
    [1,1,…,1] – n times
    and
    [1,1,…,1] – m times
    Then the resulting list should have n*m elements. You need at least O(n m) (correctly Omega(n m)) time do create such list.

    Without cartesian product (simple merge), this is quite easy. Ocaml code (I don’t know F#, should be reasonably close; compiled but not tested):

    let rec merge a b = match (a,b) with
       ([], xs) -> xs
    |  (xs, []) -> xs
    |  (x::xs, y::ys) -> if x <= y then x::(merge xs (y::ys))
                    else y::(merge (x::xs) (y::ys));;
    

    (Edit: I was too late)

    So your code in O(n m) is the best possible in worst case. However, IIUIC it performs always n*m operations, which is not optimal.

    My approach would be

    1) write a function

    group : ‘a list -> (‘a * int) list

    that counts the number of same elements:

    group [1,1,1,1,1,2,2,3] == [(1,5);(2,2);(3,1)]

    2) use it to merge both lists using similar code as before (there you can multiply those coefficients)

    3) write a function

    ungroup : (‘a * int) list -> ‘a list

    and compose those three.

    This has complexity O(n+m+x) where x is the length of resulting list. This is the best possible up to constant.

    Edit: Here you go:

    let group x =
      let rec group2 l m =
        match l with
        | [] -> []
        | a1::a2::r when a1 == a2 -> group2 (a2::r) (m+1)
        | x::r -> (x, m+1)::(group2 r 0)
      in group2 x 0;;
    
    let rec merge a b = match (a,b) with
       ([], xs) -> []
    |  (xs, []) -> []
    |  ((x, xm)::xs, (y, ym)::ys) -> if x == y then (x, xm*ym)::(merge xs ys)
                               else  if x <  y then merge xs ((y, ym)::ys)
                                               else merge ((x, xm)::xs) ys;;
    
    let rec ungroup a =
      match a with
        [] -> []
      | (x, 0)::l -> ungroup l
      | (x, m)::l -> x::(ungroup ((x,m-1)::l));;
    
    let crossjoin x y = ungroup (merge (group x) (group y));;
    
    
    
    # crossjoin [2; 4; 6; 8; 8; 10; 12] [-7; -8; 2; 2; 3; 4; 4; 8; 8; 8;];;
    - : int list = [2; 2; 4; 4; 8; 8; 8; 8; 8; 8]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of an object which need to be sorted depending on
def sortProfiles(p): return sorted(p, key=itemgetter('first_name')) I have a list with dictionaries. This function allows
I have a list of menu items which can be sorted. I have the
I have a function that needs to return a sorted list based on some
I have sorted list of strings that I move between php and java. to
I'm working in c#. I have a sorted List of structures. The structure has
I have a list of instances of the class 'Chromosome'. The list is sorted
I have a list of tuple, the list was sorted based on the first
I have a list of items sorted alphabetically: mylist = [a,b,c,d,e,f,g,h,i,j] I'm able to
I have a list of objects, that are pre-sorted based on some complex criteria

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.