![]() |
|
[Java] Why does this produce duplicates? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Why does this produce duplicates? (/showthread.php?tid=5478709) |
Why does this produce duplicates? - Tostig - Nov 28, 2016 Code import java.util.ArrayList;My output was: Code [8, 6, 7, 1, 10, 1, 2, 1, 5]Why does this not produce a result with no duplicates? * - Acteda - Nov 28, 2016 I think its because you are setting the pastint as the nextint everytime the loop goes through Meaning the nextint isnt checking for all pastints just the previous one Right? Try using a for loop / nested for loop to and keep track of it with an array instead of just one variable (hope this is clear, sorry) * - Tostig - Nov 28, 2016 (Nov 28, 2016 10:06 PM)Acteda Wrote: I think its because you are setting the pastint as the nextint everytime the loop goes through I realize the logic error AFTER posting but fuck me, for some reason using a second arraylist and checking if that arraylist contains any "used" values just does not work. No errors, it just outputs nothing. * - clrlslwi - Nov 28, 2016 (Nov 28, 2016 10:11 PM)Viktor Wrote: I realize the logic error AFTER posting but fuck me, for some reason using a second arraylist and checking if that arraylist contains any "used" values just does not work. No errors, it just outputs nothing. bruh. Code if (!printList.contains(nextint))* - Tostig - Nov 28, 2016 (Nov 28, 2016 10:14 PM)Demmonic Wrote: bruh. sat taking a shit and realized how retarded this thread was Code public static void permutate() { //void method returns nothing* - ka$h - Nov 29, 2016 (Nov 28, 2016 10:32 PM)Viktor Wrote: sat taking a shit and realized how retarded this thread was Don't be silly, taking a shit gives you super human thinking powers, along with taking showers. You can't expect yourself to comprehend this when not in super human thinking state unless you already knew it. * - Goodies - Nov 29, 2016 You're just overriding the prevint with the current one. All you're doing is making sure that adjacent numbers cannot be identical. What you SHOULD be using is a hash table. Something that will have a very fast lookup table instead of O(N) it's O(1). This will have at least one number and at most 10, but does not allow duplicates. PHP Code * - Stealth™ - Nov 29, 2016 (Nov 29, 2016 11:50 AM)Goodies Wrote: You're just overriding the prevint with the current one. All you're doing is making sure that adjacent numbers cannot be identical. If you're compiling with Java 7 or above, there's no need to define the generic type twice, the right side can be omitted. Also, SecureRandom isn't really needed in this case. If you're looking for efficiency, use ThreadLocalRandom as it's about three times faster than Random. * - +Haze - Nov 29, 2016 (Nov 28, 2016 10:32 PM)Viktor Wrote: sat taking a shit and realized how retarded this thread was Talk about useless commenting. @Op You're doing nothing to prevent duplicates in the first place anyway. * - Goodies - Nov 29, 2016 (Nov 29, 2016 03:09 PM)Stealth™ Wrote: If you're compiling with Java 7 or above, there's no need to define the generic type twice, the right side can be omitted. Also, SecureRandom isn't really needed in this case. If you're looking for efficiency, use ThreadLocalRandom as it's about three times faster than Random. I actually didn't. My IDE filled it in for me which I thought was strange, but I left it because who cares... (Click to View) As for the random thing, I'm just sticking with the devil I know. Of course if we were looking for any serious efficiency, I would be using TLR, but there's no need here. I'd be happy with either. Generating 10 numbers is nowhere near enough to actually gain a sufficient improvement. * - Tostig - Nov 29, 2016 (Nov 29, 2016 03:10 PM)+Haze Wrote: Talk about useless commenting. have to comment for the class even if its redundant read the thread i fixed it lmao |