Finding all the indexes of some elements on a given list. Can it be done in less than O(n^2) without arrays in Haskell?

Lay González

Given 2 lists of unique, orderable, non-contiguous elements, say:

['d', 'a', 'z', 'b']

I want to find their index in another list, say:

['a', 'b', 'z', 'd']

The result would be a list with their positions:

[3, 0, 2, 1]  -- element at 0 is at 3,
              -- element at 1 is at 0, etc.
András Kovács

This can be also done in O(n log n) time with a couple of sorts. I assume that the second list is a permutation of the first one.

import Data.List
import Data.Ord
import Data.Function

correspIx :: Ord a => [a] -> [a] -> [(Int, Int)]
correspIx = zip `on` map fst . sortBy (comparing snd) . zip [0..]

correspIx returns a list of pairs with the indices corresponding to each other:

correspIx "dazb" "abzd" == [(1,0),(3,1),(0,3),(2,2)]

We need another sort to get the result indicated in the question:

correspIx' :: Ord a => [a] -> [a] -> [Int]
correspIx' xs ys = map snd $ sortBy (comparing fst) $ correspIx xs ys

Now correspIx' "dazb" "abzd" == [3,0,2,1].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get the indexes of elements in a Haskell list

From Dev

Arrays with elements less or equal to the elements in given array

From Dev

Scala get all elements in a list less than a certain value

From Dev

Given two unsorted arrays A and B ,finding some pair of elements whose sum (or difference) equal to a given k - by sorting only one of the arrays

From Dev

Finding all Prime Numbers less than an input

From Dev

finding all elements in list are same or not?

From Dev

What is the time complexity for finding the maximum value less than a given value?

From Dev

Given a string, how to generate all possible arrays of substrings where each substring contains no more than N, but not less than M chars?

From Dev

Finding the number of elements less than or equal to k in a multiset

From Dev

Finding number of elements in one vector that are less than an element in another vector

From Dev

Datastructure related. Find all arrays elements greater than or equal to given input

From Dev

Finding values in elements of a list greater than X

From Dev

Group List elements with a distance less than x

From Dev

Python: Accessing elements of multi-dimensional list, given a list of indexes

From Dev

How to get a list of sub tuple if given some indexes of tuple in python?

From Dev

delete elements less than a given element in C++ set

From Dev

Finding all matching elements in a list c#

From Dev

Check if all elements in a list have a value in one of its properties - Can this be done in one line?

From Dev

Oracle "Total" plan cost is really less than some of it's elements

From Dev

MYSQL get all the results of a date less than given value

From Dev

Finding the dimension indexes of elements of an array

From Dev

Given 2 arrays, find the minimal sum of multiplication of indexes

From Dev

Finding a "complete" convex hull in less than O(n^2)

From Dev

Finding a "complete" convex hull in less than O(n^2)

From Dev

R: Paste together some string vector elements based on list of indexes

From Dev

Python: if list contains string print all the indexes / elements in the list that contain it

From Dev

How to get the indexes of column and row in a 2-dimensional matrix(list) that have the most given elements in using numpy in python

From Dev

How can I select elements lesser than a given integer, from a sorted list?

From Dev

Finding two elements from list in ascending order of given number

Related Related

  1. 1

    How can I get the indexes of elements in a Haskell list

  2. 2

    Arrays with elements less or equal to the elements in given array

  3. 3

    Scala get all elements in a list less than a certain value

  4. 4

    Given two unsorted arrays A and B ,finding some pair of elements whose sum (or difference) equal to a given k - by sorting only one of the arrays

  5. 5

    Finding all Prime Numbers less than an input

  6. 6

    finding all elements in list are same or not?

  7. 7

    What is the time complexity for finding the maximum value less than a given value?

  8. 8

    Given a string, how to generate all possible arrays of substrings where each substring contains no more than N, but not less than M chars?

  9. 9

    Finding the number of elements less than or equal to k in a multiset

  10. 10

    Finding number of elements in one vector that are less than an element in another vector

  11. 11

    Datastructure related. Find all arrays elements greater than or equal to given input

  12. 12

    Finding values in elements of a list greater than X

  13. 13

    Group List elements with a distance less than x

  14. 14

    Python: Accessing elements of multi-dimensional list, given a list of indexes

  15. 15

    How to get a list of sub tuple if given some indexes of tuple in python?

  16. 16

    delete elements less than a given element in C++ set

  17. 17

    Finding all matching elements in a list c#

  18. 18

    Check if all elements in a list have a value in one of its properties - Can this be done in one line?

  19. 19

    Oracle "Total" plan cost is really less than some of it's elements

  20. 20

    MYSQL get all the results of a date less than given value

  21. 21

    Finding the dimension indexes of elements of an array

  22. 22

    Given 2 arrays, find the minimal sum of multiplication of indexes

  23. 23

    Finding a "complete" convex hull in less than O(n^2)

  24. 24

    Finding a "complete" convex hull in less than O(n^2)

  25. 25

    R: Paste together some string vector elements based on list of indexes

  26. 26

    Python: if list contains string print all the indexes / elements in the list that contain it

  27. 27

    How to get the indexes of column and row in a 2-dimensional matrix(list) that have the most given elements in using numpy in python

  28. 28

    How can I select elements lesser than a given integer, from a sorted list?

  29. 29

    Finding two elements from list in ascending order of given number

HotTag

Archive