23
\$\begingroup\$

My boss now wants me to implement a mechanism that lets him search for an item in an array, and gives him the index/indices where that value occurs.

Your Task:

Write a program or function that receives an array and a value (String, Integer, Float, or Boolean), and returns the indices of the array at which the value occurs (either 0 or 1 indexed, whichever you prefer). If the value is not in the array return an empty array.

Input:

An array A and a value V, that may or may not be present in A.

Output:

An array containing the indice(s) at which the V occurs in A, or, if V does not occur in A, an empty array.

Test Cases:

Please note that the test cases are 0 based.

12, [12,14,14,2,"Hello World!",3,12,12]         -> [0,6,7]
"Hello World", ["Hi", "Hi World!", 12,2,3,True] -> []
"a", ["A",True,False,"aa","a"]                  -> [4]
12, [12,"12",12]                                -> [0,2]

Scoring:

This is , so the lowest score in bytes wins.

\$\endgroup\$
20
  • 1
    \$\begingroup\$ Can we assume that the given array only has one of those types (i.e. no arrays with mixed types) as many languages do not support arrays or lists with more than one type. \$\endgroup\$
    – flawr
    Aug 3, 2017 at 12:48
  • 1
    \$\begingroup\$ Sure, @flawr. You may assume that the array will only consist of values of the same type as the value to check for, if your language requires it. \$\endgroup\$
    – Gryphon
    Aug 3, 2017 at 12:49
  • 2
    \$\begingroup\$ All your arrays are 1D. Assumption? \$\endgroup\$
    – Adám
    Aug 3, 2017 at 13:44
  • 1
    \$\begingroup\$ @KevinCruijssen I meant the array to be searched. It could be multi-D. \$\endgroup\$
    – Adám
    Aug 3, 2017 at 14:25
  • 1
    \$\begingroup\$ Okay. And I'm surprised there hasn't been a language yet that can do it in 1 byte! \$\endgroup\$
    – Adalynn
    Aug 10, 2017 at 21:23

47 Answers 47

10
\$\begingroup\$

Pyth, 2 bytes

0-indexed.

xE

Try it online! or Check all the Test Cases


Explanation

xEQ  - Full Program. Takes Input from standard input. Q means evaluated input and is implicit at the end of the program.

x   - Get all the indexes of x in y
 E  - Evaluated Input #2 - The value
  Q - The list - Evaluated Input #1
\$\endgroup\$
3
  • \$\begingroup\$ You're supposed to return all occurrences not just the first. \$\endgroup\$ Aug 3, 2017 at 12:14
  • \$\begingroup\$ @EriktheOutgolfer Fixed. Just take them in reverse order. \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 12:15
  • 1
    \$\begingroup\$ Pyth is definitely the best tool for the job then :P \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 12:44
7
\$\begingroup\$

JavaScript, 39 bytes

e=>a=>[...a.keys()].filter(i=>a[i]===e)

f=
e=>a=>[...a.keys()].filter(i=>a[i]===e)

console.log(f(12)([12,14,14,2,"Hello World!",3,12,12]));
console.log(f("Hello World")(["Hi", "Hi World!", 12,2,3,true]));
console.log(f("a")(["A",true,false,"aa","a"])); 
console.log(f(12)([12,14,14,2,"Hello World!",3,12,'12']));

The above snippet might not work on all browsers, so here's a TIO link.

\$\endgroup\$
7
\$\begingroup\$

MATL, 2 bytes

mf

The m consumes two arguments, and checks each element in the array whether is equal to the other argument, f returns the indices of the truthy entries of an array.

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ It doesn't seem to work for the proposed test cases, same for the Octave solution. \$\endgroup\$
    – Cinaski
    Aug 3, 2017 at 13:41
  • \$\begingroup\$ You should use ismember instead of = to properly handle arrays of strings. mf \$\endgroup\$
    – Suever
    Aug 3, 2017 at 13:45
  • \$\begingroup\$ @LuisMendo We don't need to consider mixed input, see clarification from OP! \$\endgroup\$
    – flawr
    Aug 3, 2017 at 18:48
  • \$\begingroup\$ @flawr Oh, why is that only in a comment, and not in the challenge text? :-/ \$\endgroup\$
    – Luis Mendo
    Aug 3, 2017 at 19:01
  • \$\begingroup\$ You'd have to ask the OP, not me:) \$\endgroup\$
    – flawr
    Aug 3, 2017 at 19:13
7
\$\begingroup\$

Python 3, 45 bytes

-3 bytes thanks to @EriktheOutgolfer and @Chris_Rands

lambda y,x:[i for i,j in enumerate(x)if j==y]

Test Suite.

Today I learned enumerate(x) == zip(range(len(x)),x).


Python 3, 47 bytes

lambda n,l:[x for x in range(len(l))if l[x]==n]

Try it online! or Check all the Test Cases

\$\endgroup\$
7
  • \$\begingroup\$ Use enumerate() to bring it down a couple of bytes \$\endgroup\$ Aug 3, 2017 at 12:21
  • \$\begingroup\$ @Chris_Rands Ended up being longer. \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 12:23
  • 3
    \$\begingroup\$ lambda n,l:[x for x,y in enumerate(l)if y==n] \$\endgroup\$ Aug 3, 2017 at 12:23
  • \$\begingroup\$ I meant what @EriktheOutgolfer said \$\endgroup\$ Aug 3, 2017 at 12:25
  • 1
    \$\begingroup\$ @JonathanAllan Fixed. \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 20:28
6
\$\begingroup\$

R (+pryr), 20 bytes

pryr::f(which(a==b))

Which evaluates to the function

function (a, b) 
which(a == b)

Where either a can be the value to look for and b the vector, or the other way around. When presented with two vectors of unequal lengths (a single value counts as a length-1 vector in R), R will wrap the shorter one to match the length of the longer one. Then the equality is checked. This generates a logical vector. which provides the indices where this vector is true.

Try it online!

\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6), 44 43 bytes

Crossed out 44 is still regular 44 ;(

v=>a=>a.map((x,i)=>x===v&&++i).filter(x=>x)

Saved 1 bytes thanks to @Arnauld

let f=
v=>a=>a.map((x,i)=>x===v&&++i).filter(x=>x)
;

console.log(f(12)([12,14,14,2,"Hello World!",3,12,12]));         // => [1,7,8]
console.log(f("Hello World")(["Hi", "Hi World!", 12,2,3,true])); // => []
console.log(f("a")(["A",true,false,"aa","a"]));                  // => [5]

\$\endgroup\$
3
  • \$\begingroup\$ Can you make the === a normal == for one byte less? I came up with literally the same thing, variable names and all haha. \$\endgroup\$
    – kamoroso94
    Aug 3, 2017 at 12:51
  • 4
    \$\begingroup\$ === is necessary to distinguish 12 from "12" \$\endgroup\$
    – Christoph
    Aug 3, 2017 at 13:07
  • 1
    \$\begingroup\$ @kamoroso94 no, here's why. \$\endgroup\$ Aug 4, 2017 at 11:23
5
\$\begingroup\$

05AB1E, 4 bytes

QāsÏ

Try it online!

1-indexed.

\$\endgroup\$
3
  • \$\begingroup\$ I think both of ours mess up on inputs of: 12 and [12,'12'], unless he said it's chill for languages that don't really concrete type to not care about types. \$\endgroup\$ Aug 3, 2017 at 14:44
  • \$\begingroup\$ I actually think that 12'12' in 05AB1E because sometimes they behave differently...not sure if there's any equality test which can support such a thing though. \$\endgroup\$ Aug 3, 2017 at 14:51
  • \$\begingroup\$ If we wanted to test them for integer validity our answers would be like 60-bytes using is_alpha (a) and is_number (d), but I guess we can assume ours are valid until told otherwise. \$\endgroup\$ Aug 3, 2017 at 14:54
5
\$\begingroup\$

C#, 88 72 bytes

using System.Linq;a=>o=>a.Select((i,n)=>o.Equals(i)?n:-1).Where(n=>n>=0)

Saved 16 bytes thanks to @LiefdeWen.

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Amazing, I was still trying to figure out why i==o doesn't work. \$\endgroup\$
    – LiefdeWen
    Aug 3, 2017 at 12:29
  • 3
    \$\begingroup\$ @LiefdeWen Boxed value types. \$\endgroup\$ Aug 3, 2017 at 12:29
  • \$\begingroup\$ 72 bytes using System.Linq;a=>b=>a.Select((x,i)=>x.Equals(b)?i:-1).Where(x=>x>=0) \$\endgroup\$
    – LiefdeWen
    Aug 3, 2017 at 12:31
  • \$\begingroup\$ @LiefdeWen Nice one, I wouldn't have thought of switching it around. \$\endgroup\$ Aug 3, 2017 at 12:36
  • \$\begingroup\$ You can save a lot :) : tio.run/… \$\endgroup\$
    – digEmAll
    Jun 6, 2018 at 19:56
5
\$\begingroup\$

Jelly, 3 bytes

⁼€T

Try it online!

-1 thanks to Mr. Xcoder. (dyadic chains)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice one. I am surprised Jelly does not have a pure built-in, like Pyth does. \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 12:17
  • \$\begingroup\$ @Mr.Xcoder I think most don't. \$\endgroup\$ Aug 3, 2017 at 12:19
  • \$\begingroup\$ The irony of dyadic chains :) \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 14:19
3
\$\begingroup\$

Haskell, 41 39 bytes

v!l=fst<$>(filter((==v).snd)$zip[1..]l)

Try it online!

Saved two bytes thanks to @flawr

Haskell is statically typed, so I had to use a little workaround to run the test cases.

\$\endgroup\$
5
  • \$\begingroup\$ You don't need your workaround anymore, see the comment of the OP. \$\endgroup\$
    – flawr
    Aug 3, 2017 at 12:52
  • 1
    \$\begingroup\$ Also define a operator v#l=... instead of f v l=..., will save you two bytes:) \$\endgroup\$
    – flawr
    Aug 3, 2017 at 12:53
  • \$\begingroup\$ @flawr I had the idea of v!l=..., but didn't kow if it was accepted. I'll edit the answer. Thanks! \$\endgroup\$
    – jferard
    Aug 3, 2017 at 12:54
  • 1
    \$\begingroup\$ Using map on some filter expression is often an indicator that a list comprehension might be shorter: v!l=[i|(i,x)<-zip[1..]l,x==v]. \$\endgroup\$
    – Laikoni
    Aug 3, 2017 at 13:10
  • \$\begingroup\$ There is also a builtin, but unfortunately it is longer than Laikionis suggestion:) \$\endgroup\$
    – flawr
    Aug 3, 2017 at 13:14
3
\$\begingroup\$

Husk, 5 bytes

`fNm=

Try it online! 1-indexed.

Explanation

       -- implicitly input a value v and a list L
   m=  -- map "equals v" over the list L, resulting in a list of truthy and falsy values
`fN    -- filter the natural numbers N by discarding the numbers at falsy positions 
          and keeping the ones at truthy positions
\$\endgroup\$
6
  • \$\begingroup\$ Does this work for arrays with strings, though? \$\endgroup\$
    – 0xffcourse
    Aug 3, 2017 at 13:54
  • 1
    \$\begingroup\$ @officialaimm It works for lists containing only strings: Try it online! Lists of mixed types are not supported by Haskell and thus by Husk, but OP allowed this explicitly in the comments. \$\endgroup\$
    – Laikoni
    Aug 3, 2017 at 13:58
  • \$\begingroup\$ Is there a documentation of Husk? \$\endgroup\$
    – flawr
    Aug 3, 2017 at 14:13
  • \$\begingroup\$ @flawr Yes, it's in the wiki on the github page: github.com/barbuz/Husk/wiki \$\endgroup\$
    – Laikoni
    Aug 3, 2017 at 14:14
  • \$\begingroup\$ @flawr If you have questions about the docs of Husk in general, join us in the chatroom! \$\endgroup\$
    – Zgarb
    Aug 3, 2017 at 15:51
3
\$\begingroup\$

Ruby, 46 40 39 bytes

->e,a{i=-1;a.map{|x|i+=1;x==e&&i}-[!1]}

Saved 7 bytes!!! thanks to Eric Duminil.

Try it online.

\$\endgroup\$
1
  • \$\begingroup\$ -1 byte with !1 for false. \$\endgroup\$ Aug 3, 2017 at 18:56
3
\$\begingroup\$

Ruby, 38 bytes

->e,a{a.each_index.select{|x|a[x]==e}}

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to PPCG! \$\endgroup\$ Aug 4, 2017 at 14:20
3
\$\begingroup\$

Google Sheets, 101 bytes

=IfError(Join(",",Filter(Column(Offset(A1,0,0,1,Counta(Split(B1,",")))),Exact(Split(B1,","),A1))),"")

Value V in A1 and array A in B1 with each entry separated by a comma. Null entires are not allowed (row 5 below shows what happens).

Result

Explanation:

Offset(A1,0,0,1,Counta(Split(B1,","))) returns a range that is one row tall and as many columns wide as there are entries in A1.

=IfError(Join(",",Filter(Column(~),Exact(Split(B1,","),A1))),"") filters the column numbers of that range based on whether or not the value in A1 is exactly each of the values in B1 and concatenates them all in a comma-delineated list.

\$\endgroup\$
3
\$\begingroup\$

Clojure, 40 bytes

First attempt at code golf.

keep-indexed maps a function over a collection here, passing the current index into the callback and yielding any non-nil return values.

(fn[a b](keep-indexed #(if(= %2 a)%1)b))

Try it online!

\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog Unicode), 2 bytesSBCS

Takes item to look for as left argument (must be scalar to find an item of the lookup array rather than a subarray) and the lookup array (which may have up to 15 dimensions) as right argument. Returns list of indices, each of which may has as many elements as the number of dimensions in the lookup array.

⍸⍷

Try it online!

ɩndices where

 found

\$\endgroup\$
5
  • \$\begingroup\$ I was about to say it ties Pyth, but you know... Unicode. Wouldn't this be 2 bytes in APL Dyalog Classic (since it uses SBCS)? \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 14:08
  • \$\begingroup\$ @Mr.Xcoder isn't in the character set. Still, since Dyalog uses way less than 256 unique chars, it could have been a single byte. When we add new glyphs, we refrain from changing the character set so that backwards compatibility is maintained. \$\endgroup\$
    – Adám
    Aug 3, 2017 at 14:13
  • \$\begingroup\$ Ah, Thanks! (I have no idea how APL / Dyalog works) \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 14:15
  • \$\begingroup\$ @Mr.Xcoder APL is a commercial language (not a golfing language), so Dyalog have certain obligations to existing subscribers. \$\endgroup\$
    – Adám
    Aug 3, 2017 at 14:19
  • \$\begingroup\$ APL isn't a golfing language, but there do exist open-source APL implementations (ngn and GNU). \$\endgroup\$
    – Adalynn
    Aug 5, 2017 at 16:32
2
\$\begingroup\$

Batch, 86 bytes

@set i=0
:g
@if "%~2"=="" exit/b
@if %1==%2 echo %i%
@set/ai+=1
@shift/2
@goto g

Takes input as command line parameters (value then the array elements as separate parameters). Note: String quoting is considered part of the match e.g. "1" won't equal 1 (would cost 6 bytes).

\$\endgroup\$
2
\$\begingroup\$

Python 2, 49 bytes

lambda l,v:filter(lambda i:l[i]==v,range(len(l)))

Try it online!

Not short enough, but I thought it was cool. ¯\_(ツ)_/¯

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 28 bytes

sub{grep$_[$_]eq$_[0],1..@_}

Try it online!

The output is 1-indexed.
An anonymous function is quite unusual for Perl, but it happens to be the shortest I could think of. grep ..., 1 .. @_ iterates over the indexes of the input array (actually it goes one cell beyond the last, but it doesn't matter), keeping only the index that satisfy $_[$_]eq$_[0], ie. the ones where the value of the element ($_[$_]) is the same as the value we need to keep ($_[0]).


Slightly longer (31 bytes (30 + -l flag)), but as a full program:

$@=<>;$@eq$_&&print$.-1while<>

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Haskell, 37 33 bytes

import Data.List
findIndices.(==)

Thanks @Laikoni for -4 bytes!

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Pointfree is shorter: findIndices.(==) \$\endgroup\$
    – Laikoni
    Aug 3, 2017 at 13:15
  • \$\begingroup\$ Oh right, that is even more pointfree, thanks=) \$\endgroup\$
    – flawr
    Aug 3, 2017 at 13:16
  • 1
    \$\begingroup\$ Why not elemIndices? \$\endgroup\$
    – nimi
    Aug 3, 2017 at 17:53
2
\$\begingroup\$

Java 8, 146 113 112 111 110 108 bytes

import java.util.*;l->o->{List r=new Stack();for(int i;(i=l.indexOf(o))>-1;l.set(i,null))r.add(i);return r;}

-2 bytes thanks to @TAsk by using Vector instead of ArrayList.
-1 byte by using Stack instead of Vector.
-2 bytes thanks to @Jakob by inputting a ArrayList instead of an array.

0-indexed

Explanation:

Try it here.

import java.util.*;    // Required import for Vector and Vector
l->o->{                // Method with List and Object parameters
  List r=new Stack();  //  Result-list
  for(int i;(i=l.indexOf(o))>=-1;
                       //  Loop as long as we can find the object in the list
    l.set(i,null))     //   After every iteration, remove the found item from the list
      r.add(i);        //    Add the index to the result-list
                       //  End of loop (implicit / single-line body)
  return r;            //  Return the result-List
}                      // End of method
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Cool! If I am not wrong Vector may save few bytes. :) \$\endgroup\$
    – akash
    Aug 4, 2017 at 5:48
  • 1
    \$\begingroup\$ @TAsk Thanks! Need to remember that one. I use List+ArrayList pretty often. \$\endgroup\$ Aug 4, 2017 at 7:35
  • 1
    \$\begingroup\$ List r=new Vector(); will work, too. \$\endgroup\$
    – akash
    Aug 4, 2017 at 8:41
  • 1
    \$\begingroup\$ You can save 1 byte by taking a list instead: TIO. Seems like a small enough change not to merit a separate answer. \$\endgroup\$
    – Jakob
    Aug 22, 2017 at 1:03
  • \$\begingroup\$ The change breaks searching for null, but that's fine. \$\endgroup\$
    – Jakob
    Aug 22, 2017 at 1:04
1
\$\begingroup\$

05AB1E, 4 bytes

Qƶ0K

Try it online!

It is 1-indexed, as shown below:

IN A-#------------------------> [2,3,3,3,4]
IN B-#------------------------> 3
-----#------------------------+-----------------
Q    # Vectorized equivalence | [0,1,1,1,0]
 ƶ   # Lift by index          | [0,2,3,4,0]
  0K # Remove zeros           | [2,3,4]
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 12 bytes

Position@##&

1-Indexed

input [Array,Value]

[{12, 14, 14, 2, "Hello World!", 3, 12, 12}, 12]

output

{{1}, {7}, {8}}

\$\endgroup\$
1
  • \$\begingroup\$ Why not just Position? \$\endgroup\$
    – hftf
    Sep 25, 2017 at 4:06
1
\$\begingroup\$

Haskell, 29 bytes

e#l=[i|(i,h)<-zip[0..]l,h==e]    

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Does that work with the heterogeneous input cases? (Mixtures of integers, strings, a "true" value, etc). \$\endgroup\$
    – Kaz
    Aug 4, 2017 at 15:56
  • \$\begingroup\$ @Kaz: no, it doesn't. It's polymorphic and works for every type where equality is defined for, but all list elements have to be of the same type. According to a comment in the OP that's enough. \$\endgroup\$
    – nimi
    Aug 4, 2017 at 16:02
1
\$\begingroup\$

Japt, 9 bytes

mȶV©YÄÃf

1-indexed.

Japt input doesn't support booleans, so they have been replaced with 0 and 1 in the test cases.

Try it online! with the -Q flag to format the array output.

0-indexed Solution, 11 bytes

l o f@gX ¶V

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ One of the few times rather than ¥ comes in handy :P I was thinking of doing something along the lines of m@Y*(X¶V} f, but I hadn't realized that wouldn't work for index 0. 1-indexing is clever... \$\endgroup\$ Aug 4, 2017 at 1:06
1
\$\begingroup\$

Perl 6, 21 bytes

{grep :k,*===$^v,@^z}

Try it online!

The :k adverb to grep tells it to return the matching keys (indices) of the input sequence that match the predicate * === $^v.

If strings and numbers were considered equivalent, one could use a grep predicate of just $^v instead of * === $^v.

\$\endgroup\$
1
  • \$\begingroup\$ eqv might be better than === depending on what you want to consider equivalent values. \$\endgroup\$ Aug 7, 2017 at 17:21
1
\$\begingroup\$

Common Lisp, 66 bytes

(lambda(x s)(loop as i in s as j from 0 when(equal i x)collect j))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

TXR Lisp, 26 bytes

(op where(op equal @@1)@2)

In other words, "Where is argument 2 equal to argument 1?"

Run:

1> (op where(op equal @@1) @2)
#<interpreted fun: lambda (#:arg-01-0166 #:arg-02-0167 . #:rest-0165)>
2> [*1 12 #(12 14 14 2 "Hello world!" 3 12 12)]
(0 6 7)
3> [*1 "Hello World" #("Hi" "Hi world!" 12 2 3 t)]
nil
\$\endgroup\$
1
\$\begingroup\$

Clojure, 39 38 bytes

#(filter(comp #{%2}%)(range(count %)))

A bit obscure :) The first input argument is a vec of values and the second one is the searched value. % maps indexes to values, and the set #{%2} returns truthy (the input argument %2) or falsy nil for that value. comp composes these together.

\$\endgroup\$
1
\$\begingroup\$

C 340 362 166 115 Bytes

Hello all. My first time here. I figured since I enjoy (attempting) to write optimized code I may as well give this a try.

@Rodney - ~39 bytes from the includes

@Zacharý - 7 bytes with implicit typing

0-indexed.

How to Run:

As per @Arnolds suggestion, the program takes arguments in a much more C friendly manner. This let me reduce the size of the file by a little more than half.

The arguments should be passed in the following order value [element1 ...] where braces indicate optional arguments

You may or may not have to add escaped quotes to any strings that are provided in order to satisfy the condition of 12 != "12". On my system the this can be done in the following manner

prog-name.exe 12 3 "Hello" 12 4 "12"
Returns [2,4]     < This is incorrect

prog-name.exe 12 3 "\"Hello\"" 12 4 "\"12\""
Returns [2]       < Correct

golfed

#define P printf(
b=0;main(int c,char**v){P"[");for(--c;c-1;c--)b|=strcmp(v[1],v[c])?0:P b?",%i":"%i",c-2);P"]");}

ungolfed

#define P printf(

//Implicit only works in global(I totally knew this after almost 4 years of C :P)
b = 0;
main(int c,char**v)
{

    P"[");

    //match loop
    //b is used to determine if this is the first iteration. it can be assumed that printf will always return >0
    //subract two from c to get correct index number of match
    for(--c; c-1; c--)
        b |= strcmp(v[1], v[c]) ? 0 : P b ? ",%i" : "%i", c-2);

    P"]");

    return 0;
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to the site. I notice you have a lot of extra whitespace. Particularly around operators i = 0. These can be removed. I suggest playing around with the whitespace a bit. \$\endgroup\$
    – Wheat Wizard
    Aug 4, 2017 at 1:15
  • \$\begingroup\$ With the way you handle the list, a first argument of ,12 and second argument of [12,14,14,2,"Hello World!",3,12,12] prints [5,6] which is technically incorrect. \$\endgroup\$ Aug 4, 2017 at 2:21
  • \$\begingroup\$ @ArnoldPalmer I updated the code to make it a little more verbose at detecting data types. However, since C doesn't have all the fancy type conversion such as JavaScript, it is still vulnerable to having a comma in a 'number' type. I pretty much just left it assuming correctly formatted input. \$\endgroup\$
    – Marcos
    Aug 4, 2017 at 7:22
  • \$\begingroup\$ @Marcos There's a chance you may be able to take each value of the array as it's own command line argument. I don't golf in C ever, so not quite sure what the rules are, but it doesn't seem unreasonable to me that you'd be allowed to do that. Especially since accepting the array as a list leaves you vulnerable to this problem. Also, you still have a bit of white space in your golfed code. You don't need the spaces on the #include statements, strstr(h+i,n)-h ==i has an extra space, and you can do return-1 instead of return -1. \$\endgroup\$ Aug 4, 2017 at 10:22
  • \$\begingroup\$ are implicit declarations allowed? I think you can ditch the #include statements \$\endgroup\$
    – Rodney
    Aug 5, 2017 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.