r/dailyprogrammer • u/rya11111 3 1 • Mar 20 '12
[3/20/2012] Challenge #28 [easy]
The array duplicates problem is when one integer is in an array for more than once.
If you are given an array with integers between 1 and 1,000,000 or in some other interval and one integer is in the array twice. How can you determine which one?
Your task is to write code to solve the challenge.
Note: try to find the most efficient way to solve this challenge.
13
Upvotes
10
u/oskar_s Mar 20 '12 edited Mar 20 '12
Ok, so there are a few basic ways to do this, so I thought I'd list the algorithms you can use to solve this and why they are good or bad. Going from simplest and least efficient to most efficient, they are:
Method number 1: Loop through each element of the array, and then loop through it again to see if there are any duplicates. This is the most obvious way (and if you're looping through the array and using the "count" or "index" function, this is what you're doing). You can speed this up by noting that on the second, inner loop, you only need to check indexes higher than the variable on the first loop. In python:
Method number 2: Sort the list first, and then check adjacent elements to see if they are equal. If they are, that's your duplicate. One of the problems with this is that you've destroyed the original order of the list, and you might not want to do that. The solution is to copy the list into a new array and with each value, store the original position in the array with it, and then sort this new list. In python:
Method number 3: Keep a hashtable around. Loop through the list and for each element, first check if it's in the hashtable as a key. If it is, you've found a duplicate, and you can look at the hashtable's value to find out the position of the other element. If it's not, store the element of the value you are at in the hashtable, and use the index as the value. This way, you loop through the array, and for each element you add it to the hashtable, and you can detect when there are collisions. In python:
Now, what are the running times here? Well, the first one has two nested loops, and the number of checks it has to perform is 1 + 2 + 3 + 4 + ... + 999999 + 1000000. That is, the number of checks are the triangular numbers, n(n+1)/2, which is obviously O( n2 ).
The second one first sorts the list, which is O(n log n), and then loops through it once, which is O(n), so the running time is O(n log n)
For the third one, remember that adding and looking up keys into hashtables are both O(1), so since you loop through it only once, the running time is O(1) * O(n) which is equal to O(n). That means that the hashtable method, method 3, is by far the fastest.
I've written a python script that tests all three methods and times them, if anyone is curious to try. It can be found at http://pastebin.com/i8ycPpw0
The results are as follows (and I'm testing this on the worlds worst computer, your results will probably be better): for a million element array, the hashtable method takes 0.004 seconds, which is so small it's more or less within the margin of error of nothing. I'm guessing the vast majority of time taken there is used for printing to the screen. The sorting method takes 0.025 seconds, which is still blazingly fast, but still more than five times slower than the hashtable method. The naive method, the method most programmers would use, takes 6.75 seconds, which is more than one thousand times as slow as the hashtable method! I know it's simple to just use the "array.index(value)" function, but doing that is really, really bad practice. In fact, while it is also O( n2 ), it is twice as slow as the slightly optimized version I presented here.
And that's the end of today's lecture!
EDIT: Damn you, reddit formatting!
1
u/healxph0enix Mar 20 '12
Okay so I am still somewhat new. I deal with the duplicate problem by simply taking it out ?
1
u/RandomWorkAccount Mar 20 '12
is the array sorted or not?
3
u/[deleted] Mar 21 '12
Perl using hash
1
u/gtklocker Mar 20 '12
Python:
2
u/huck_cussler 0 0 Mar 21 '12 edited Mar 21 '12
EDIT:
OK, I guess I could have used Java's sort since it's surely faster than the one I wrote anyhow. Here is the much more simplified version:
And here is the novel I wrote before looking at other peoples' solutions.
Java:
This one was awesomely ass-kicking for me. I'm looking forward to looking at others' solutions, especially since mine is (as usual) much longer than normal.
Here are just the methods used to find the dupe. Since the description wanted efficiency, I did it the most efficient way I knew how. Namely, I wrote a quicksort to first put the array in order, then wrote a simple method to step through the sorted array until we find the two that are the same.
And here is the rest of the class, which is code to create the array, shuffle it all up, create one random duplicate, then re-sort it with the duplicate hidden in there, and call the method to find the duplicate.
I'd be anxious to hear some feedback on all of this. The code seems pretty fast, but is there an easier way to do all of this that I'm missing?
1
u/luxgladius 0 0 Mar 20 '12
Perl
O(n)=n log(n)
1
u/[deleted] Mar 21 '12
Javascript 1.6
Sort and nested loops having already been discussed, I tried a diminishing search as each iteration decreases the amount of the array searched.
1
u/snaders Mar 21 '12
What's wrong with this in PHP? Where $a and $b are the two arrays:
1
u/school_throwaway Mar 21 '12
Python, should work at a larger scale but only used 10 numbers to test it, am pretty sure it isn't very efficient though.
1
u/met48 Mar 21 '12
Python
1
u/namekuseijin Mar 23 '12 edited Mar 23 '12
plain R4RS Scheme. returns the indexes of the duplicates items.
no, I don't need no stinking vector.
1
u/JerMenKoO 0 0 Apr 09 '12
J:
1
u/stinktank Mar 20 '12
actual sum - (1,000,000 * 1,000,001 / 2) ?
-1
u/snoozebar Mar 20 '12 edited Mar 20 '12
Python
-1
u/Steve132 0 1 Mar 20 '12
Python, two lines, O(n)