3
\$\begingroup\$

A basket is full with \$n\$ colored balls. For each color except one, there are at least 2 balls with that color. Find the color which only has one ball. in \$O(n)\$ space complexity.

What if each ball is repeated exactly twice?

You can consider the input to be an array or a list or any container of colors as strings with a maximum length of 100

This is so the shortest code in bytes wins.

\$\endgroup\$
5
  • 7
    \$\begingroup\$ Unless you specify some kind of input and output format (or the flexibility of what you allow) people are going to make vastly different assumptions. \$\endgroup\$ Commented Jul 9, 2012 at 18:24
  • 2
    \$\begingroup\$ This question isn't suited for CodeGolf. You need to specify a winning criteria. Otherwise, try StackOverflow.com if you are needing help. \$\endgroup\$
    – MrZander
    Commented Jul 9, 2012 at 21:15
  • \$\begingroup\$ Do you want us to find the duplicate balls as the title suggests? \$\endgroup\$ Commented Jul 10, 2012 at 15:00
  • \$\begingroup\$ @PrinceJohnWesley Yes thats the whole idea!! \$\endgroup\$ Commented Jul 10, 2012 at 15:36
  • \$\begingroup\$ -1 no input format specified, no output format specified... How you output the repeated ball? Index? Is it one indexed or zero indexed? \$\endgroup\$ Commented Jul 8, 2016 at 22:31

13 Answers 13

10
\$\begingroup\$

Python - 17 chars

Solely as an expression:

min(B,key=B.count)

Full, standalone program with input and output, 34 chars:

B=input()
print min(B,key=B.count)

edit: What gnibbler said.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Python3 would be a little shorter - use input instead of raw_input and () around the print. Since the input format isn't specified, why not assign single letter codes to each colour? eg "rrgbb", then you can drop the split(). And use min instead of sorted FTW \$\endgroup\$
    – gnibbler
    Commented Jul 11, 2012 at 23:23
  • \$\begingroup\$ Thanks! Yeah, I guess a string is within the bounds of 'any container' :D The question doesn't even mention input, so I think it's legit with 2.x's input() and have the user wrap quotes around it too. \$\endgroup\$
    – daniero
    Commented Jul 11, 2012 at 23:51
6
\$\begingroup\$

K, 17 15

&1=#:'=" "\:0:0

Takes input from stdin as a space separated list

k)&1=#:'=" "\:0:0
red red green blue blue
"green"
\$\endgroup\$
5
  • \$\begingroup\$ I think you've got me beat this time - it takes me 15 characters just to get the separate strings. \$\endgroup\$
    – Gareth
    Commented Jul 11, 2012 at 21:36
  • \$\begingroup\$ ...or maybe not - I've got the input down to 8 which at least gives me a fighting chance... :-) \$\endgroup\$
    – Gareth
    Commented Jul 11, 2012 at 21:43
  • \$\begingroup\$ Given the way you successively golfed the last one I wouldn't be surprised to see you beat me again. My solution can't get any shorter, I think. \$\endgroup\$
    – tmartin
    Commented Jul 11, 2012 at 21:57
  • \$\begingroup\$ No, I think 20 is my limit here. \$\endgroup\$
    – Gareth
    Commented Jul 11, 2012 at 22:15
  • \$\begingroup\$ No need to wrap this in a function, &1=#:'=" "\:0:0 runs fine for 15. \$\endgroup\$
    – skeevey
    Commented Jul 12, 2012 at 16:32
5
\$\begingroup\$

J, 31 24 20 characters

{.(~./:#/.~);:1!:1[1

A lot prettier than the previous attempt. Takes input from keyboard.

Makes an assumption that wasn't necessary in the previous version - that there is exactly one colour with one ball. If there are 2 colours with one ball you'll get the first that appears in the list, if there are no colours with only one ball you'll get the colour with the least balls.

Example:

   {.(~./:#/.~);:1!:1[1
green green red blue blue
┌───┐
│red│
└───┘
\$\endgroup\$
3
\$\begingroup\$

SQL - 44

Find Unique Color

SELECT C FROM B GROUP BY C HAVING COUNT(C)=1

Find Duplicate Colors

SELECT C FROM B GROUP BY C HAVING COUNT(C)>1
\$\endgroup\$
1
  • \$\begingroup\$ The right tool for the job! \$\endgroup\$ Commented Jul 8, 2016 at 22:32
2
\$\begingroup\$

Python 2.7, 44 chars

Using the same sort of format as Keith Randall's answer of a function that takes an array and returns the element/colour that only occurs once.

R=lambda B:[a for a in B if B.count(a)<2][0]

Alternatively, if I need to read in colours from the command line and output the colour that is unique (which takes 60 chars):

B=raw_input().split();print[a for a in B if B.count(a)<2][0]
\$\endgroup\$
4
  • \$\begingroup\$ This is O(1) space complexity - which is usually better than O(n), but technically isn't what the question asks for :) \$\endgroup\$
    – gnibbler
    Commented Jul 10, 2012 at 23:32
  • 2
    \$\begingroup\$ I think you could use <2 rather than ==1 to save a char. \$\endgroup\$
    – grc
    Commented Jul 11, 2012 at 1:24
  • \$\begingroup\$ @gnibbler: O(1) is a subset of O(n). \$\endgroup\$ Commented Jul 11, 2012 at 3:00
  • \$\begingroup\$ @grc: Thanks, amended the solution. \$\endgroup\$
    – JPvdMerwe
    Commented Jul 11, 2012 at 9:22
2
\$\begingroup\$

Clojure - 44

#(some(fn[[c n]](if(= n 1)c))(frequencies%))

That's a lambda function. Example:

(#(some(fn[[c n]](if(= n 1)c))(frequencies%))
  [:red :red :red :blue :green :blue :yellow :yellow])
=> :green
\$\endgroup\$
0
1
\$\begingroup\$

Mathematica 23

n contains the list.

Cases[Tally@n,{x_,1}:>x]
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 23

After a tremendous amount of help from w0lf, here's the source code.

' '/:x.&{x{1$=},,1=\;},

DEMO


28 characters with the error checking

? is displayed if there is not one unique color. For the sake of keeping the number of characters down to a minimum, the question mark was used (this of course can be changed to something more meaningful).

' '/:x.&{x{1$=},,1=\;},'?'or

DEMO


Commentary

' '/:x        # split up each element and assign it to 'x'
.&            # copy the last element in the stack and do a "setwise and"
{x            # start filtration process and invoke 'x'
{1$=}         # checks for an equality comparison
,,            # find the count of each unique character
1=            # compare value to '1'
\;            # leave only value that has count of '1' on the stack
},            # close filtration process and print element on stack
'?'or         # OPTIONAL: is printed if no element with a count of '1' is found
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 2 bytes

.m

Try it online!

.m is a builtin function which return the least frequent element in a list.

If you are not allowed to use a builtin to solve the challenge, then it's 4 2 bytes:

-2 thanks to @caird coinheringaahing

ʒ¢

Try it online!

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

Python 2.7, 67 chars

from collections import*
R=lambda B:Counter(B).most_common()[-1][0]
\$\endgroup\$
0
\$\begingroup\$

R - 18 chars

which(table(x)==1)
\$\endgroup\$
0
\$\begingroup\$

APL (14)

B/⍨1=+/B∘.≡B←⎕

Takes input from the keyboard in the form of an APL list, so you need to put quotes around the colors 'like' 'this'. (Or use numbers.)

      B/⍨1=+/B∘.≡B←⎕
⎕:
      'green' 'green' 'red' 'blue' 'blue'
 red 

As a function (that takes a list, also 14 characters):

{⍵/⍨1=+/⍵∘.≡⍵}
\$\endgroup\$
0
\$\begingroup\$

Groovy - 22 chars

it.min{a->it.count(a)}

Attribution and test case:

lone={it.min{a->it.count(a)}}

l = ['red','green', 'red', 'blue', 'green']

assert lone(l) == 'blue'
assert lone(l) != 'red'
\$\endgroup\$
2
  • \$\begingroup\$ You might want to put a link to TIO, like Try it online! \$\endgroup\$ Commented Dec 27, 2020 at 7:00
  • \$\begingroup\$ @CommandMaster Keep in mind that this question and answer were posting 8 years ago, and that the answerer hasn't been on the site since 2018 \$\endgroup\$ Commented Jan 1, 2021 at 15:03

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