# Find the indices of values in one list in another

You must take two lists of positive integers as input, let's call these n and m.

You may assume that:

• All integers in n are part of m
• All integers in m are unique
• The lists are non-empty

### Challenge: Return the indices of where you find the values in n, in m.

That might be confusing, but I think the test cases will make the task pretty clear. The examples are 1-indexed, you may choose 0-indexed if you want to (please specify).

n = 5 3 4 1
m = 6 8 4 1 2 5 3 100
output: 6 7 3 4    // 5 is in the 6th position of m
// 3 is in the 7th position of m
// 4 is in the 3rd position of m
// 1 is in the 4th position of m

n = 5 3 4 9 7 5 7
m = 3 4 5 7 9
output: 3 1 2 5 4 3 4

n = 1 2 3 4 5 6
m = 1 2 3 4 5 6
output: 1 2 3 4 5 6

n = 16 27 18 12 6 26 11 24 26 20 2 8 7 12 5 22 22 2 17 4
m = 15 18 11 16 14 20 37 38 6 36 8 32 21 2 31 22 33 4 1 35 3 25 9 30 26 39 5 23 29 10 13 12 7 19 24 17 34 27 40 28
output: 4 38 2 32 9 25 3 35 25 6 14 11 33 32 27 16 16 14 36 18

n = 54
m = 54
output: 1


The winners will be the shortest solutions in each language.

This is a very nice meta-post by the way!

• This might be a strange question, but would it be OK to assume the input will have a trailing space? – James May 1 '17 at 17:57
• Curious why you ask, but yeah, sure... – Stewie Griffin May 1 '17 at 17:59

# V, 26 bytes

jòdf kÄ/-
DÓÓ
ÒC1@"Gòdk


Try it online!

This is a very strange and hacky solution, because V has little to no concept of numbers. Input comes in this format:

6 8 4 1 2 5 3 100
5 3 4 1


With a trailing space on each line.

Hexdump:

00000000: 6af2 6466 206b c42f 122d 0a44 d3d3 0ad2  j.df k./.-.D....
00000010: 0143 311b 4022 47f2 646b                 .C1.@"G.dk


Explanation:

j                   " Move down one line (to N) (1)
ò                  " Recursively:
df                "   (d)elete until you (f)ind a space. This will be saved into
"   register '-' (2)
k              "   Move up one line (to M)
Ä             "   Duplicate line M (3)
/<C-r>-      "   Move the cursor forward until the next occurence of register '-'
"   (the number we deleted from N)
"   (4)
D                   "   Delete every character *after* the cursor (5)
ÓÓ                 "   Remove everything on this line except for whitespace
Ò<C-a>              "   Replace every character on this line with <C-a>, which is the
"   command for incrementing a number (6)
C             "   Delete this line into register '"', and enter insert mode
1<esc>       "   Enter a '1' and return to normal mode
@"     "   Run register '"' as V code (7)
G    "   Go to the last line (1)
ò   " End recursion
dk " Delete the last two lines (m and n)


If this doesn't make it clearer, here are examples of the buffer during the various stages the loop goes through:

Stage 1 (| is the cursor)

6 8 4 1 2 5 3 100
|5 3 4 1


Stage 2:

6 8 4 1 2 5 3 100
|3 4 1


Stage 3:

|6 8 4 1 2 5 3 100
6 8 4 1 2 5 3 100
3 4 1


Stage 4:

6 8 4 1 2 |5 3 100
6 8 4 1 2 5 3 100
3 4 1


Stage 5:

6 8 4 1 2 |
6 8 4 1 2 5 3 100
3 4 1


Stage 6:

|<C-a><C-a><C-a><C-a><C-a>
6 8 4 1 2 5 3 100
3 4 1


Stage 7:

|6
6 8 4 1 2 5 3 100
3 4 1


Back to stage 1:

6
6 8 4 1 2 5 3 100
|3 4 1


# Python 2, 25 bytes

lambda a,b:map(a.index,b)


Try it online!

Note that this uses 0-indexing.

# APL (Dyalog), 1 byte

⍳


Try it online!

Note: the ⍳ function does not take scalars as its left argument, so to give it a left argument like 54, you have to make it into an array using , like so (,54).

## Mathematica, 25 bytes

#&@@@PositionIndex@#/@#2&


Takes two inputs m and n, and returns the 1-based indices of n in m.

# Retina, 3231 30 bytes

1 byte saved thanks to Kritixi Lithos and 1 byte thanks to Martin Ender

(\d+)(?=.*¶(\d+ )*\1 )
$#2 G1  Uses 0-indexing. Input has a trailing space on each line. Try it online! Explanation (\d+)(?=.*¶(\d+ )*\1 )$#2


Here we replace every number on the first line by the number of numbers before the same number on the second line.

G1


Then, we delete the second line, leaving only the new first line as the output.

# Java, 104 81 bytes

1 byte thanks to Business cat.

void f(int[]a,int[]b){for(int i=0,j=0;i<b.length;)j=a[j++]==b[i]?0*(b[i++]=j):j;}


Try it online!

# C#, 32 Bytes

(n,m)=>n.Select(i=>m.IndexOf(i))


This is the code as a lambda expression, so it should be valid.

The solution is with a 0 based index. I think it's pretty straigt forward how it works - it simply takes the items of n and selects the indices of the items in m.

# Octave, 25 bytes

@(n,m)([x,~]=find(n==m'))


Try it online!

# Haskell, 32 bytes

a%b=[length$fst$span(/=x)b|x<-a]


Try it online! One-indexed.

Other attempts:

q(h:t)x|x==h=0|1>0=1+q t x;map.q
f b=map$length.fst.($b).span.(/=)
a%b=[until((==x).(b!!))(+1)0|x<-a]
a%b=[until(\y->x==b!!y)(+1)0|x<-a]
import Data.List;map.flip elemIndex


# k, 1

This is a built-in operator in k and uses zero-based indexing.

?


Example:

k)6 8 4 1 2 5 3 100 ? 5 3 4 1
5 6 2 3


# 05AB1E, 5 bytes

v²yk,


Try it online!

v     # For each value in n (call it y)
²    # Push m
y   # Push y
k, # Print the 0-indexed index of y in m


# Jelly, 3 bytes

iÐ€


Try it online!

## Specs

• Input: two arguments, the first being m, and the second being n.
• Output: one array

## JavaScript (ES6), 28 bytes

Takes the arrays in currying syntax (n)(m). 0-indexed.

let f =

n=>m=>n.map(v=>m.indexOf(v))

console.log(JSON.stringify(f([5,3,4,1])([6,8,4,1,2,5,3,100])))
console.log(JSON.stringify(f([5,3,4,9,7,5,7])([3,4,5,7,9])))
console.log(JSON.stringify(f([1,2,3,4,5,6])([1,2,3,4,5,6])))
console.log(JSON.stringify(f([16,27,18,12,6,26,11,24,26,20,2,8,7,12,5,22,22,2,17,4])([15,18,11,16,14,20,37,38,6,36,8,32,21,2,31,22,33,4,1,35,3,25,9,30,26,39,5,23,29,10,13,12,7,19,24,17,34,27,40,28])))
console.log(JSON.stringify(f([54])([54])))

# Perl 6, 31 bytes

->\n,\m{n.map:{m.first($_,:k)}}  Try it ## Expanded: -> \n, \m { # pointy block lambda n.map: { # map over the values in ｢n｣ m.first($_, :k ) # return the key ｢:k｣ of the first occurrence
}
}


0 indexed

# Japt, 4 bytes

m!bV


Test it online!

### Explanation

There's not much to explain here, but it shows off an interesting feature of Japt. Normally, you would pass a function to m, like so:

mX{VbX}


This is basically U.map(X => V.indexOf(X)) (the U is implicit). However, when you're just performing one operation between two values (b here, on V and X), you can just give the operator and the other value and Japt will make a function out of it. This means mX{X+2} can be golfed to m+2.

However, this doesn't work when the values are in the wrong order (mbV would be short for mX{XbV}). To get around this, you can prepend an exclamation mark to the operator, which tells Japt to swap the operands. This costs an extra byte, but it's still a couple bytes shorter than the alternative. And now you know a little more about Japt.

# MATL, 2 bytes

&m


This uses 1-indexing. Try it online!

### Explanation

The meta-function & indicates that the next function will use a (function-specific) secondary default in/out specification. For function m (ismember), & specifies that its second output will be produced. This contains the index of (the first occurrence of) each entry of the first input in the second input.

## Haskell, 34 bytes

n#m=[i|a<-n,(i,e)<-zip[1..]m,e==a]


Usage example: [5,3,4,9,7,5,7] # [3,4,5,7,9] -> [3,1,2,5,4,3,4]

The built-in elemIndex is in Data.List and therefore longer than the version above. The outer loop goes through n and the inner loop through pairs of (i,e) where i is the index of e in m. Keep the i where e equals the current element of n.

# R, 20 5 bytes

1-indexed; match is the builtin function that finds the indices in the second input of the elements of the first, i.e., match(n,m) gives the desired answer

match


thanks to @flodel for pointing out that returning a function is perfectly acceptable as an answer!

Try it online!

• I think match (5 bytes) alone would be an acceptable solution. – flodel May 2 '17 at 11:07
• you are correct, updated. – Giuseppe May 3 '17 at 19:30

# Pyth, 4 bytes

xLQE


Try it online!

Note that this uses 0-indexing.

# J, 2 bytes

i.


This is not a complete program, but a built-in function.

Use it as such:

echo 6 8 4 1 2 5 3 100 i. 5 3 4 1


Try it online!

Note that this uses 0-indexing.

# CJam, 4 bytes

{f#}


Anonymous block that expects arguments on the stack and leaves the result on the stack.

Uses 0-indexing.

Try it online!

## Haskell, 43 bytes

a*b=[[fst x|x<-zip[0..]b,y==snd x]!!0|y<-a]

a*b=                                         -- define function * with 2 args
[                                |y<-a]  -- for each elt in first arg
zip[0..]b                     -- match elts in second arg w/ idxs
-- [a,b,c] -> [[0,a],[1,b],[2,c]]
[fst x|x<-                  ]           -- take first element in each pair
,y==snd x            -- if the index matches
!!0        -- first element (always only 1)


## Clojure, 25 bytes

#(map(zipmap %2(range))%)


0-indexed.

# Perl 5, 38 34 bytes

4 bytes saved thanks to Dada

sub{map$x{$_}//($x{$_}=++$x)x0,@_}  1-indexed. Takes the lists m and n as a single list, like f(@m,@n). The x0 is just to keep the output from starting with 1,2,3,4,5, etc. • Nice answer. Note that anonymous functions are allowed, so sub{...} can save you 2 bytes. Also, you can use x0 instead of &&() to save two more bytes. – Dada May 2 '17 at 10:56 # PHP, 56 Bytes Online Versions 0 Indexing output as String <?foreach($_GET[0]as$v)echo" ".array_flip($_GET[1])[$v];  ## PHP, 65 Bytes Output as array <?foreach($_GET[0]as$v)$r[]=array_flip($_GET[1])[$v];print_r($r);  PHP, 78 Bytes workaround with array_map <?print_r(array_map(function($v){return array_flip($_GET[1])[$v];},$_GET[0]));  for not unique arrays replace with array_flip($_GET[1])[$v] array_search($v,$_GET[1]) # Bash + coreutils, 51 for n in$1
do grep -wn $n <<<"$2"
done|cut -d: -f1


s()(sort -$1k2) nl|s|join -j2 - <(nl<<<"$1"|s)|s n|cut -d\  -f3


# Java 7, 80 bytes

void c(int[]a,java.util.List b){for(int i=0;i<a.length;a[i]=b.indexOf(a[i++]));}


0-indexed

Explanation:

void c(int[]a,java.util.List b){  // Method with integer-array and List parameters
for(int i=0;i<a.length;         //  Loop over the integer-array
a[i]=b.indexOf(a[i++])        //   And change every value to the index of the List
);                              //  End of loop (no body)
}                                 // End of method


Test code:

Try it here.

import java.util.Arrays;
class M{
static void c(int[]a,java.util.List b){for(int i=0;i<a.length;a[i]=b.indexOf(a[i++]));}

public static void main(String[] a){
int[] x = new int[]{ 5, 3, 4, 1 };
c(x, Arrays.asList(6, 8, 4, 1, 2, 5, 3, 100));
System.out.println(Arrays.toString(x));

x = new int[]{ 5, 3, 4, 9, 7, 5, 7 };
c(x, Arrays.asList(3, 4, 5, 7, 9));
System.out.println(Arrays.toString(x));

x = new int[]{ 1, 2, 3, 4, 5, 6 };
c(x, Arrays.asList(1, 2, 3, 4, 5, 6));
System.out.println(Arrays.toString(x));

x = new int[]{ 16, 27, 18, 12, 6, 26, 11, 24, 26, 20, 2, 8, 7, 12, 5, 22, 22, 2, 17, 4 };
c(x, Arrays.asList(15, 18, 11, 16, 14, 20, 37, 38, 6, 36, 8, 32, 21, 2, 31, 22, 33, 4, 1, 35, 3, 25, 9, 30, 26, 39, 5, 23, 29, 10, 13, 12, 7, 19, 24, 17, 34, 27, 40, 28));
System.out.println(Arrays.toString(x));

x = new int[]{ 54 };
c(x, Arrays.asList(54));
System.out.println(Arrays.toString(x));
}
}


Output:

[5, 6, 2, 3]
[2, 0, 1, 4, 3, 2, 3]
[0, 1, 2, 3, 4, 5]
[3, 37, 1, 31, 8, 24, 2, 34, 24, 5, 13, 10, 32, 31, 26, 15, 15, 13, 35, 17]
[0]