Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does. If there are no such elements, your program / function may result in undefined behaviour.
Example:
For a = [2, 3, 3, 1, 5, 2]
, the output should be
firstDuplicate(a) = 3
.
There are 2 duplicates: numbers 2 and 3. The second occurrence of 3 has a smaller index than the second occurrence of 2 does, so the answer is 3.
For a = [2, 4, 3, 5, 1]
, the output should be
firstDuplicate(a) = -1
.
This is code-golf, so shortest answer in bytes wins.
BONUS: Can you solve it in O(n) time complexity and O(1) additional space complexity?
-
\$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$Martin Ender– Martin Ender2017年09月03日 13:12:36 +00:00Commented Sep 3, 2017 at 13:12
69 Answers 69
Python 2, 34 bytes
O(n2) time, O(n) space
Saved 3 bytes thanks to @vaultah, and 3 more from @xnor!
lambda l:l[map(l.remove,set(l))<0]
-
1\$\begingroup\$ It looks like
lambda l:l[map(l.remove,set(l))<0]
works, even though the order of evaluation is weird. \$\endgroup\$xnor– xnor2017年07月31日 00:08:32 +00:00Commented Jul 31, 2017 at 0:08 -
\$\begingroup\$ This doesn't return
-1
when no duplicates are found without the 'footer code', does that code not count towards the bytes? I'm new to code golf, sorry if it's a basic question! \$\endgroup\$Chris_Rands– Chris_Rands2017年07月31日 12:38:59 +00:00Commented Jul 31, 2017 at 12:38 -
\$\begingroup\$ @Chris_Rands Beneath the question musicman did ask if exception is okay instead of -1 and OP said its okay and musicman's answer throws exception. \$\endgroup\$LiefdeWen– LiefdeWen2017年07月31日 13:55:51 +00:00Commented Jul 31, 2017 at 13:55
-
\$\begingroup\$ That took me a while to figure out. Well played. Getting the 0th element of l using the conditional after modifying it is really clever. \$\endgroup\$Thoth19– Thoth192017年07月31日 22:45:40 +00:00Commented Jul 31, 2017 at 22:45
-
\$\begingroup\$ Does Python guarantee the time and space complexity of standard-library functions like set.remove? \$\endgroup\$Draconis– Draconis2017年08月01日 02:30:39 +00:00Commented Aug 1, 2017 at 2:30
JavaScript (ES6), (削除) 47 (削除ここまで) (削除) 36 (削除ここまで) (削除) 31 (削除ここまで) 25 bytes
Saved 6 bytes thanks to ThePirateBay
Returns undefined
if no solution exists.
Time complexity: O(n) :-)
Space complexity: O(n) :-(
a=>a.find(c=>!(a[-c]^=1))
How?
We keep track of already encountered values by saving them as new properties of the original array a by using negative numbers. This way, they can't possibly interfere with the original entries.
Demo
let f =
a=>a.find(c=>!(a[-c]^=1))
console.log(f([2, 3, 3, 1, 5, 2]))
console.log(f([2, 4, 3, 5, 1]))
console.log(f([1, 2, 3, 4, 1]))
-
\$\begingroup\$ 25 bytes:
a=>a.find(c=>!(a[-c]^=1))
\$\endgroup\$user72349– user723492017年07月30日 22:11:22 +00:00Commented Jul 30, 2017 at 22:11 -
\$\begingroup\$ @ThePirateBay Oh, of course. Thanks! \$\endgroup\$Arnauld– Arnauld2017年07月30日 22:14:02 +00:00Commented Jul 30, 2017 at 22:14
-
\$\begingroup\$ Just notice that Objects in JavaScript may not be implemented as hash table. Time complexity of accessing keys of some object may not be O(1). \$\endgroup\$tsh– tsh2017年08月01日 09:47:03 +00:00Commented Aug 1, 2017 at 9:47
Mathematica, 24 bytes
#/.{h=___,a_,h,a_,h}:>a&
Mathematica's pattern matching capability is so cool!
Returns the original List
for invalid input.
Explanation
#/.
In the input, replace...
{h=___,a_,h,a_,h}
A List
with a duplicate element, with 0 or more elements before, between, and after the duplicates...
... :>a
With the duplicate element.
Jelly, 5 bytes
Ṛœ-QṪ
How it works
Ṛœ-QṪ Main link. Argument: A (array)
Ṛ Yield A, reversed.
Q Unique; yield A, deduplicated.
œ- Perform multiset subtraction.
This removes the rightmost occurrence of each unique element from reversed
A, which corresponds to the leftmost occurrence in A.
Ṫ Take; take the rightmost remaining element, i.e., the first duplicate of A.
-
\$\begingroup\$
œ-
removes the rightmost occurrences? TIL \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月31日 09:42:13 +00:00Commented Jul 31, 2017 at 9:42 -
\$\begingroup\$ This doesn't seem to return
-1
for no duplicates. Throwing an exception is okay as per OP but I'm not sure if0
is even though it's not in the range. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月31日 12:18:34 +00:00Commented Jul 31, 2017 at 12:18
Pyth, 5 bytes
h.-Q{
Remove from Q the first appearance of every element in Q, then return the first element.
-
\$\begingroup\$ @LuisMendo Ok thanks. Sorry for creating confusion, I should learn to read... \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年07月31日 11:53:43 +00:00Commented Jul 31, 2017 at 11:53
-
\$\begingroup\$ @Mr.Xcoder No, it's the OP's fault. That information should be in the challenge text, but just in a comment \$\endgroup\$Luis Mendo– Luis Mendo2017年07月31日 12:40:47 +00:00Commented Jul 31, 2017 at 12:40
Haskell, 35 bytes
f s(h:t)|h`elem`s=h|1<2=f(h:s)t
f[]
Try it online! Crashes if no duplicate is found.
Vyxal, 4 bytes
UÞ⊍h
Takes the multi-set symmetric difference, which outputs the duplicate values in order that they occur. Outputs 0
if nothing is found.
Jelly, 6 bytes
xŒQ¬$Ḣ
Returns the first duplicate, or 0 if there is no duplicate.
Explanation
xŒQ¬$Ḣ Input: array M
$ Operate on M
ŒQ Distinct sieve - Returns a boolean mask where an index is truthy
for the first occurrence of an element
¬ Logical NOT
x Copy each value in M that many times
Ḣ Head
-
\$\begingroup\$ It's golfier to use indexing like this:
ŒQi0ị
. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年07月31日 12:23:10 +00:00Commented Jul 31, 2017 at 12:23 -
\$\begingroup\$ @EriktheOutgolfer If there are no duplicates,
i0
would return 0, whereị
would index and return the last value of the input instead of 0. \$\endgroup\$miles– miles2017年07月31日 12:31:21 +00:00Commented Jul 31, 2017 at 12:31
Japt, 7 bytes
æ@bX ¦Y
Explanation
æ@ bX ¦ Y
UæXY{UbX !=Y} Ungolfed
Implicit: U = input array
UæXY{ } Return the first item X (at index Y) in U where
UbX the first index of X in U
!=Y is not equal to Y.
In other words, find the first item which has already occured.
Implicit: output result of last expression
Alternatively:
æ@ ̄Y øX
Explanation
æ@ ̄ Y øX
UæXY{Us0Y øX} Ungolfed
Implicit: U = input array
UæXY{ } Return the first item X (at index Y) in U where
Us0Y the first Y items of U (literally U.slice(0, Y))
øX contains X.
In other words, find the first item which has already occured.
Implicit: output result of last expression
Dyalog APL, (削除) 27 (削除ここまで) (削除) 24 (削除ここまで) (削除) 20 (削除ここまで) (削除) 19 (削除ここまで) (削除) 13 (削除ここまで) (削除) 12 (削除ここまで) 11 bytes
⊢⊃⍨0⍳⍨⊢=⍴↑∪
Now modified to not depend on v16! Try it online!
How? (With input N)
⊢⊃⍨...
- N at this index:⍴↑∪
- N with duplicates removed, right-padded with0
to fit N⊢=
- Element-wise equality with N0⍳⍨
- Index of the first0
. `
-
\$\begingroup\$ nevermind, I misread the question. not enough test cases though... \$\endgroup\$Uriel– Uriel2017年07月30日 21:44:20 +00:00Commented Jul 30, 2017 at 21:44
-
\$\begingroup\$ Sorry for misleading you, I also misread the question. \$\endgroup\$miles– miles2017年07月30日 22:12:05 +00:00Commented Jul 30, 2017 at 22:12
-
\$\begingroup\$ Looks like 36 bytes to me. \$\endgroup\$Adám– Adám2017年07月31日 11:09:36 +00:00Commented Jul 31, 2017 at 11:09
-
\$\begingroup\$ Oh god, iota underbar isn't in
⎕AV
, is it? \$\endgroup\$Adalynn– Adalynn2017年07月31日 11:22:24 +00:00Commented Jul 31, 2017 at 11:22 -
\$\begingroup\$ @Zacharý Right, Classic translates it to
⎕U2378
when loading. Try it online! \$\endgroup\$Adám– Adám2017年08月04日 04:44:28 +00:00Commented Aug 4, 2017 at 4:44
Brachylog, 5 bytes
a⊇=bh
Explanation
a⊇=bh Input is a list.
a There is an adfix (prefix or suffix) of the input
⊇ and a subsequence of that adfix
= whose elements are all equal.
b Drop its first element
h and output the first element of the rest.
The adfix built-in a
lists first all prefixes in increasing order of length, then suffixes in decreasing order of length.
Thus the output is produced by the shortest prefix that allows it, if any.
If a prefix has no duplicates, the rest of the program fails for it, since every subsequence of equal elements has length 1, and the first element of its tail doesn't exist.
If a prefix has a repeated element, we can choose the length-2 subsequence containing both, and the program returns the latter.
-
\$\begingroup\$ Another 5 bytes solution:
a⊇Ċ=h
, which only looks at length-2 subsets. \$\endgroup\$Fatalize– Fatalize2017年12月01日 07:20:02 +00:00Commented Dec 1, 2017 at 7:20
Python 3, (削除) 94 (削除ここまで) 92 bytes
O(n) time and O(1) extra memory.
def f(a):
r=-1
for i in range(len(a)):t=abs(a[i])-1;r=[r,i+1][a[t]<0>r];a[t]*=-1
return r
Explanation
The basic idea of the algorithm is to run through each element from left to right, keep track of the numbers that have appeared, and returning the number upon reaching a number that has already appeared, and return -1 after traversing each element.
However, it uses a clever way to store the numbers that have appeared without using extra memory: to store them as the sign of the element indexed by the number. For example, I can represent the fact that 2
and 3
has already appeared by having a[2]
and a[3]
negative, if the array is 1-indexed.
-
\$\begingroup\$ What would this do for
i
where a[i] > n? \$\endgroup\$Downgoat– Downgoat2017年07月31日 06:02:36 +00:00Commented Jul 31, 2017 at 6:02 -
\$\begingroup\$ @Downgoat read the question again. \$\endgroup\$Leaky Nun– Leaky Nun2017年07月31日 06:04:53 +00:00Commented Jul 31, 2017 at 6:04
-
\$\begingroup\$ The question says
1
to a.length but for a[i]= a.length wouldn't this go out of bounds? \$\endgroup\$Downgoat– Downgoat2017年07月31日 06:05:56 +00:00Commented Jul 31, 2017 at 6:05 -
\$\begingroup\$ @Downgoat
t=abs(a[i])-1=a.length-1
\$\endgroup\$Leaky Nun– Leaky Nun2017年07月31日 06:09:10 +00:00Commented Jul 31, 2017 at 6:09 -
3\$\begingroup\$ Note from feersum: "solution is cheating because it uses integers 1 bit larger than the input." \$\endgroup\$Leaky Nun– Leaky Nun2017年07月31日 06:22:00 +00:00Commented Jul 31, 2017 at 6:22
Perl 6, 13 bytes
*.repeated[0]
Explanation
The
*
is in a Term position so the whole statement is a WhateverCode lambda.The
.repeated
is a method that results in every value except for the first time each value was seen.say [2, 3, 3, 3, 1, 5, 2, 3].repeated.perl; # (3, 3, 2, 3).Seq # ( 3, 3, 2, 3).Seq
[0]
just returns the first value in the Seq.
If there is no value Nil is returned.
(Nil is the base of the Failure types, and all types are their own undefined value, so Nil different than an undefined value in most other languages)
Note that since the implementation of .repeated
generates a Seq that means it doesn't start doing any work until you ask for a value, and it only does enough work to generate what you ask for.
So it would be easy to argue this has at worst O(n) time complexity, and at best O(2) time complexity if the second value is a repeat of the first.
Similar can probably be said of memory complexity.
J, 12 bytes
,&_1{~~:i.0:
Explanation
,&_1{~~:i.0: Input: array M
~: Nub-sieve
0: The constant 0
i. Find the index of the first occurrence of 0 (the first duplicate)
,&_1 Append -1 to M
{~ Select the value from the previous at the index of the first duplicate
APL (Dyalog), 20 bytes
⊃n/⍨(,≢∪) ̈,\n←⎕,2⍴ ̄1
2⍴ ̄1
negative one reshaped into a length-two list
⎕,
get input (mnemonic: console box) and prepend to that
n←
store that in n
,\
prefixes of n (lit. cumulative concatenation)
(
...) ̈
apply the following tacit function to each prefix
,
[is] the ravel (just ensures that the prefix is a list)
≢
different from
∪
the unique elements[?] (i.e. is does the prefix have duplicates?)
n/⍨
use that to filter n (removes all elements until the first for which a duplicate was found)
⊃
pick the first element from that
-
\$\begingroup\$ Wow, you got beat three times. Still, +1. And can you add an explanation of how this works? \$\endgroup\$Adalynn– Adalynn2017年08月03日 22:10:12 +00:00Commented Aug 3, 2017 at 22:10
-
\$\begingroup\$ @Zacharý Apparently I just needed to get the ball rolling. Here you go. \$\endgroup\$Adám– Adám2017年08月04日 05:13:07 +00:00Commented Aug 4, 2017 at 5:13
-
\$\begingroup\$ @Zacharý Eventually, I managed to beat them all. \$\endgroup\$Adám– Adám2017年08月04日 08:09:12 +00:00Commented Aug 4, 2017 at 8:09
APL (Dyalog), 11 bytes
As per the new rules, throws an error if no duplicates exist.
⊢⊃⍨⍬⍴⍳∘≢~⍳⍨
⍳⍨
the indices of the first occurrence of each element
~
removed from
⍳∘≢
of all the indices
⍬⍴
reshape that into a scalar (gives zero if no data is available)
⊃⍨
use that to pick from (gives error on zero)
⊢
the argument
-
\$\begingroup\$ Well, yeah, when the rules are changed, of course you can beat them all! \$\endgroup\$Adalynn– Adalynn2017年08月04日 20:05:07 +00:00Commented Aug 4, 2017 at 20:05
-
\$\begingroup\$ Well, I tied you. \$\endgroup\$Adalynn– Adalynn2017年08月04日 20:56:01 +00:00Commented Aug 4, 2017 at 20:56
APL, 15
{⊃⍵[(⍳⍴⍵)~⍵⍳⍵]}
Seems like we can return 0 instead of -1 when there are no duplicates, (thanks Adám for the comment). So 3 bytes less.
A bit of description:
⍵⍳⍵ search the argument in itself: returns for each element the index of it's first occurrence
(⍳⍴⍵)~⍵⍳⍵ create a list of all indexes, remove those found in ⍵⍳⍵; i.e. remove all first elements
⊃⍵[...] of all remaining elements, take the first. If the array is empty, APL returns zero
For reference, old solution added -1 to the list at the end, so if the list ended up empty, it would contain -1 instead and the first element would be -1.
{⊃⍵[(⍳⍴⍵)~⍵⍳⍵], ̄1}
Try it on tryapl.org
-
\$\begingroup\$ You may return a zero instead of
¯1
, so{⊃⍵[(⍳⍴⍵)~⍵⍳⍵]}
should do. \$\endgroup\$Adám– Adám2017年08月04日 05:29:08 +00:00Commented Aug 4, 2017 at 5:29
Retina, (削除) 26 (削除ここまで) 24 bytes
1!`\b(\d+)\b(?<=\b1円 .*)
Try it online! Explanation: \b(\d+)\b
matches each number in turn, and then the lookbehind looks to see whether the number is a duplicate; if it is the 1
st match is !
output, rather than the count of matches. Unfortunately putting the lookbehind first doesn't seem to work, otherwise it would save several bytes. Edit: (削除) Added 7 bytes to comply with the Saved 2 bytes thanks to @MartinEnder.-1
return value on no match. (削除ここまで)
-
2\$\begingroup\$ For the record, the lookaround won't backtrack. This prevents this from working if you try to put it before. I've made this mistake many times, and Martin always corrects me. \$\endgroup\$FryAmTheEggman– FryAmTheEggman2017年07月31日 00:37:47 +00:00Commented Jul 31, 2017 at 0:37
-
\$\begingroup\$ I got 30 bytes by using a lookahead instead of a lookbehind. Also, the rules now say you don't need to return
-1
. \$\endgroup\$Value Ink– Value Ink2017年08月04日 07:45:40 +00:00Commented Aug 4, 2017 at 7:45 -
\$\begingroup\$ @ValueInk But the correct answer for that test case is 3... \$\endgroup\$Neil– Neil2017年08月04日 08:00:19 +00:00Commented Aug 4, 2017 at 8:00
-
\$\begingroup\$ OH. I misread the challenge, whoops \$\endgroup\$Value Ink– Value Ink2017年08月04日 08:47:46 +00:00Commented Aug 4, 2017 at 8:47
PHP, (削除) 56 44 38 (削除ここまで) 32 bytes
for(;!${$argv[++$x]}++;);echo$x;
Run like this:
php -nr 'for(;!${$argv[++$x]}++;);echo$x;' -- 2 3 3 1 5 2;echo
> 3
Explanation
for(
;
!${ // Loop until current value as a variable is truthy
$argv[++$x] // The item to check for is the next item from input
}++; // Post increment, the var is now truthy
);
echo $x; // Echo the index of the duplicate.
Tweaks
- Saved 12 bytes by using variables instead of an array
- Saved 6 bytes by making use of the "undefined behavior" rule for when there is no match.
- Saved 6 bytes by using post-increment instead of setting to 1 after each loop
Complexity
As can be seen from the commented version of the code, the time complexity is linear O(n)
. In terms of memory, a maximum of n+1
variables will be assigned. So that's O(n)
.
-
\$\begingroup\$ Thanks for not using a weird encoding. But you should add the
error_reporting
option to the byte count (or use-n
, which is free). \$\endgroup\$Titus– Titus2017年08月11日 11:33:15 +00:00Commented Aug 11, 2017 at 11:33 -
\$\begingroup\$ We've been here before. PHP notices and warnings are ignorable. I might as well pipe them to
/dev/null
, which is the same. \$\endgroup\$aross– aross2017年08月11日 13:02:34 +00:00Commented Aug 11, 2017 at 13:02 -
\$\begingroup\$ I tend to remember the wrong comments. :) Isn´t this O(n)? \$\endgroup\$Titus– Titus2017年08月11日 13:04:42 +00:00Commented Aug 11, 2017 at 13:04
-
\$\begingroup\$ Yes it's linear \$\endgroup\$aross– aross2017年08月11日 13:05:05 +00:00Commented Aug 11, 2017 at 13:05
-
\$\begingroup\$ How is that
O(1)
for additional space? You're literally assigning a new variable pern
, which isO(n)
\$\endgroup\$Xanderhall– Xanderhall2017年08月11日 14:46:37 +00:00Commented Aug 11, 2017 at 14:46
K (ngn/k), 11 bytes
*(~':?',\)#
*(~':?',\)#
( )# keep the elements where the left function returns true
(when applied to the whole array):
,\ prefixes
?' uniquify each
~': is it the same as previous?
* first element
R, 34 bytes
c((x=scan())[duplicated(x)],-1)[1]
Cut a few characters off the answer from @djhurio, don't have enough reputation to comment though.
-
\$\begingroup\$ oh...I didn't see this answer; this is good for the prior spec when missing values required
-1
but with the new spec, I managed to golf it down even more. This is still solid and it's a different approach from the way he did it, so I'll give you a +1! \$\endgroup\$Giuseppe– Giuseppe2017年07月31日 14:22:45 +00:00Commented Jul 31, 2017 at 14:22
J, (削除) 17 (削除ここまで) 16 bytes
(*/{_1,~i.&0)@~:
How?
(*/{_1,~i.&0)@~:
@~: returns the nub sieve which is a vector with 1 for the first occurrence of an element in the argument and 0 otherwise
i.&0 returns the first index of duplication
_1,~ appends _1 to the index
*/ returns 0 with duplicates (product across nub sieve)
{ select _1 if no duplicates, otherwise return the index
-
\$\begingroup\$ I think you can now return
NA
for missing values since the spec has changed; so(x=scan())[duplicated(x)][1]
is perfectly valid. \$\endgroup\$Giuseppe– Giuseppe2017年07月31日 12:53:46 +00:00Commented Jul 31, 2017 at 12:53
Dyalog APL Classic, 18 chars
Only works in ⎕IO←0
.
w[⊃(⍳∘≢~⍳⍨)w← ̄1,⎕]
Remove from the list of indices of the elements of the argument with a prepended "-1" the list indices of its nub and then pick the first of what's left. If after the removal there only remains an empty vector, its first element is by definition 0 which is used to index the extended argument producing the desired -1.
-
\$\begingroup\$ Um... what's with the random leading spaces? +1 for outgolfing me by one byte. \$\endgroup\$Adalynn– Adalynn2017年08月03日 22:07:39 +00:00Commented Aug 3, 2017 at 22:07
-
\$\begingroup\$ You may throw an error instead of returning
¯1
, so you can remove¯1,
and use⎕IO←1
. \$\endgroup\$Adám– Adám2017年08月04日 05:30:44 +00:00Commented Aug 4, 2017 at 5:30
Ruby, (削除) 28 (削除ここまで) 36 bytes
Misunderstood the challenge the first time. O(n)
time, O(n)
space.
->a{d={};a.find{|e|b=d[e];d[e]=1;b}}
Java (OpenJDK 8), (削除) 65 (削除ここまで) (削除) 117 (削除ここまで) 109 bytes
Previous 65 byte solution:
r->{for(int a,b=0,z,i=0;;b=a)if((a=b|1<<(z=r[i++]))==b)return z;}
New solution. 19 bytes are included for import java.math.*;
-8 bytes thanks to @Nevay
r->{int z,i=0;for(BigInteger c=BigInteger.ZERO;c.min(c=c.setBit(z=r[i++]))!=c;);return z;}
Edit
The algorithm in my original program was fine, but the static size of the datatype used meant that it broke fairly quickly once the size went above a certain threshold.
I have changed the datatype used in the calculation to increase the memory limit of the program to accommodate this (using BigInteger
for arbitrary precision instead of int
or long
). However, this makes it debatable whether or not this counts as O(1)
space complexity.
I will leave my explanation below intact, but I wish to add that I now believe it is impossible to achieve O(1)
space complexity without making some assumptions.
Proof
Define N
as an integer such that 2 <= N
.
Let S
be a list representing a series of random integers [x{1}, ..., x{N}]
, where x{i}
has the constraint 1 <= x{i} <= N
.
The time complexity (in Big-O notation) required to iterate through this list exactly once per element is O(n)
The challenge given is to find the first duplicated value in the list. More specifically, we are searching for the first value in S
that is a duplicate of a previous item on the list.
Let p
and q
be the positions of two elements in the list such that p < q
and x{p} == x{q}
. Our challenge becomes finding the smallest q
that satisfies those conditions.
The obvious approach to this problem is to iterate through S and check if our x{i}
exists in another list T
:
If x{i}
does not exist in T
, we store it in T
.
If x{i}
does exist in T
, it is the first duplicate value and therefore the smallest q
, and as such we return it.
This space efficiency is O(n)
.
In order to achieve O(1)
space complexity while maintaining O(n)
time complexity, we have to store unique information about each object in the list in a finite amount of space. Because of this, the only way any algorithm could perform at O(1)
space complexity is if:
1. N is given an upper bound corresponding to the memory required to store the maximum number of possible values for a particular finite datatype.
2. The re-assignment of a single immutable variable is not counted against the complexity, only the number of variables (a list being multiple variables).
3. (Based on other answers) The list is (or at least, the elements of the list are) mutable, and the datatype of the list is preset as a signed integer, allowing for changes to be made to elements further in the list without using additional memory.
1 and 3 both require assumptions and specifications about the datatype, while 2 requires that only the number of variables be considered for the calculation of space complexity, rather than the size of those variables. If none of these assumptions are accepted, it would be impossible to achieve both O(n)
time complexity and O(1)
space complexity.
Explanation
Whoo boy, this one took (削除) an embarrassingly long time to think up (削除ここまで) a bit of brain power.
So, going for the bonus is difficult. We need both to operate over the entire list exactly once and track which values we've already iterated over without additional space complexity.
Bit manipulation solves those problems. We initialize our O(1)
'storage', a pair of integers, then iterate through the list, OR-ing the ith bit in our first integer and storing that result to the second.
For instance, if we have 1101
, and we perform an OR operation with 10
, we get 1111
. If we do another OR with 10
, we still have 1101
.
Ergo, once we perform the OR operation and end up with the same number, we've found our duplicate. No duplicates in the array causes the program to run over and throw an exception.
-
\$\begingroup\$ Also, your second test includes the number 100, but thats impossible since the array itself is only 5 long \$\endgroup\$SchoolBoy– SchoolBoy2017年08月10日 14:54:34 +00:00Commented Aug 10, 2017 at 14:54
-
-
\$\begingroup\$ @SchoolBoy Good catch. My only problem is that there doesn't seem to be any upper limit on the size of the array, so I can't realistically change my code to solve memory issues. \$\endgroup\$Xanderhall– Xanderhall2017年08月10日 16:26:01 +00:00Commented Aug 10, 2017 at 16:26
-
\$\begingroup\$ @Xanderhall True, but i feel like 32 (or if you use a long, 64) numbers is too little :p. Either way, imposing a limit on the input, and then allocating the maximum memory needed and calling it O(1) memory is just a cheat. It is still O(n) since if the size of the input increased, so would this upper bound to the memory. Which is also why I think it is impossible to create an O(n) O(1) algorithm \$\endgroup\$SchoolBoy– SchoolBoy2017年08月10日 16:29:05 +00:00Commented Aug 10, 2017 at 16:29
-
\$\begingroup\$ @Xanderhall P.S. I'm getting close to your 65, I'm at 67 bytes :p \$\endgroup\$SchoolBoy– SchoolBoy2017年08月10日 16:29:50 +00:00Commented Aug 10, 2017 at 16:29
Java 8, (削除) 82 (削除ここまで) (削除) 78 (削除ここまで) (削除) 76 bytes (削除ここまで) No longer viable, (削除) 75 (削除ここまで) (削除) 67 (削除ここまで) 64 bytes below in edit
As a lambda function:
a->{Set<Long>s=new HashSet<>();for(long i:a)if(!s.add(i))return i;return-1;}
Probably can be made much smaller, this was very quick.
Explanation:
a->{ //New lambda function with 'a' as input
Set<Long>s=new HashSet<>(); //New set
for(long i:a) //Iterate over a
if(!s.add(i)) //If can't add to s, already exists
return i; //Return current value
return-1; //No dupes, return -1
}
*Edit*
(削除) 75 (削除ここまで) (削除) 67 (削除ここまで) 64 bytes using the negation strategy:
a->{int i=0,j;while((a[j=Math.abs(a[i++])-1]*=-1)<0);return++j;}
(-3 bytes thanks to @Nevay)
Explanation:
a->{ //New lambda expression with 'a' as input
int i=0,j; //Initialise i and declare j
while((a[j=Math.abs(a[i++])-1]*=-1)<0); //Negate to keep track of current val until a negative is found
return++j; //Return value
}
Loops over the array, negating to keep track. If no dupes, just runs over and throws an error.
Both of these work on O(n) time and O(n) space complexity.
-
\$\begingroup\$ It's worth noting that this will need to be assigned to a lambda returning
Number
, sincei
is along
and-1
is anint
. \$\endgroup\$Jakob– Jakob2017年08月10日 14:36:02 +00:00Commented Aug 10, 2017 at 14:36 -
\$\begingroup\$ @Jakob Not necessary, -1 being an int will automatically be cast to a long without explicitly specifying the cast \$\endgroup\$SchoolBoy– SchoolBoy2017年08月10日 14:47:51 +00:00Commented Aug 10, 2017 at 14:47
-
\$\begingroup\$ It will cast implicitly to
long
, but not toLong
as required for the lambda to be assigned to aFunction
. Did you test it? Regardless, that solution can be replaced with your new one. \$\endgroup\$Jakob– Jakob2017年08月10日 16:13:59 +00:00Commented Aug 10, 2017 at 16:13 -
1\$\begingroup\$ You can use raw types
Set s=new HashSet();
to save 7 bytes. (Besides: afaik the import ofjava.util.*;
has to be included into the byte count -> +19 bytes.) The return statement can bereturn++j
, the if-statement can be removeda->{int i=0,j;for(;(a[j=Math.abs(a[i++])-1]*=-1)<0;);return++j;}
(-3 bytes). \$\endgroup\$Nevay– Nevay2017年08月11日 13:34:24 +00:00Commented Aug 11, 2017 at 13:34
Husk, 4 bytes
←Ṡ-u
Returns 0
if no input contains no duplicate, try it online!
Explanation
←Ṡ-u -- takes a list X as input & returns 0 or first duplicate
Ṡ- -- compute X - ...
u -- ... deduplicate X
← -- get first element or 0 if empty
Haskell, 34 bytes
import Data.List
f x=head$x\\nub x
Try it online! For an input x = [2,3,3,1,5,2]
, nub x
removes duplicates [2,3,1,5]
and x\\nub x
removes the elements of nub x
from x, thus keeping the duplicates [3,2]
. head
returns the first element of hat list.
Point-free is the same byte count: head.((\\)<*>nub)
.
MATL, 8 bytes
&=Rsqf1)
Gives an error (without output) if no duplicate exists.
Try at MATL Online!
Explanation
&= % Implict input. Matrix of all pairwise equality comparisons
R % Keep the upper triangular part (i.e. set lower part to false)
s % Sum of each column
q % Subtract 1
f % Indices of nonzero values
1) % Get first. Gives an error is there is none. Implictly display