Python, 143 bytes
There are already some very good Python answers out there (see @dieters, @ppperry & @xnor answers) so I decided to take a different approach, have some fun with recursion and python lambda functions and see what could I come up with. This is not even close to the best answers here but it was fun to think.
The program takes the integer list as a parameter of function p and returns a list containing non-unique elements.
p=lambda i:[j for j in(lambda n:[]if len(n)<=1 else[(lambda x:x[0]if len([1 for o in x[1:]if x[0]==o])==1 else 'N')(n)]+p(n[1:]))(i) if j!='N']
Two line version for better readability:
p=lambda i:[j for j in(lambda n:[]if len(n)<=1 else[(lambda x:x[0]if
len([1 for o in x[1:]if x[0]==o])==1 else 'N')(n)]+p(n[1:]))(i) if j!='N']
Short Explanation
The inner lambda takes a list x as a parameter and returns the first element of such list if it is repeated only once in the rest of the list (x[1:]). If not it returns 'N'.
The lambda in between the outer and inner lambdas is the one in charge of the recursive search. This returns a list containing the non-unique elements and plenty of 'N's. This is the one which takes the integer list as a parameter.
The outer lambda filters the resulting list getting rid of the unwanted 'N's.
Test cases
p([])
[]
p([-1,0,1])
[]
p([3, 0, 0, 1, 1, 0, 5, 3])
[3,0,1]
4Related – Sp3000 – 2015-10-13T10:11:37.837
1since this is for array of integers code would be different. I think much shorter. That is for a string. – garg10may – 2015-10-13T10:21:01.480
1Are we allowed to accept input as lines instead of as an array? For example, instead of
[-1, 0, 1], can we input (replace \n with newlines):"-1\n0\n1"? – Addison Crump – 2015-10-13T15:01:39.4131Does the output have to be a list or would a set be acceptable? – Dennis – 2015-10-13T15:06:12.190
And does it have to output in that format? – Addison Crump – 2015-10-13T15:06:59.623
Yes, the output needs to be an array, otherwise if iterable is allowed people would start producing
301as results. – garg10may – 2015-10-14T01:59:47.760