Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java method that determines duplicates in array
New Posts  All Forums:Forum Nav:

Java method that determines duplicates in array - Page 2

post #11 of 15
Thread Starter 
I got it to work!!

Code:
public static boolean hasDuplicates(int[] values)
    {
         boolean found = false;
         for (int i = 0; i < values.length; i++){
             for (int r = i+1; r < values.length; r++){
                 if (values[i] == values[r]){
                     found = true;
                 }
             }
         }
         return found;
    }
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 4200+  ASUS M2N-SLI Deluxe eVGA Geforce 7900GT CO Corsair XMS2 4GB PC2-6400 
Hard DriveOptical DriveOSMonitor
Seagate 160GB SATA2 + Western Digital 250GB SATA2 Lite-On SATA DVD Burner Linux Mint 14 Cinnamon x64   
PowerCase
OCZ GameXStream 700w Antec P180 
  hide details  
Reply
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 4200+  ASUS M2N-SLI Deluxe eVGA Geforce 7900GT CO Corsair XMS2 4GB PC2-6400 
Hard DriveOptical DriveOSMonitor
Seagate 160GB SATA2 + Western Digital 250GB SATA2 Lite-On SATA DVD Burner Linux Mint 14 Cinnamon x64   
PowerCase
OCZ GameXStream 700w Antec P180 
  hide details  
Reply
post #12 of 15
Quote:
Originally Posted by AMD_RULES View Post
I got it to work!!

Code:
public static boolean hasDuplicates(int[] values)
    {
        
         for (int i = 0; i < values.length; i++){
             for (int r = i+1; r < values.length; r++){
                 if (values[i] == values[r]){
                     return true;
                 }
             }
         }
         return false;
    }


Congrats, sorry i didn't see this before would have given you a hand. You can probably just return true, or return false instead of declaring a boolean value in the method. Makes it a look a little cleaner in my opinion.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #13 of 15
Quote:
Originally Posted by CovertCover View Post
Eww, my professor said the same thing, yet the book were learning out of uses break statements quite liberally. Well, you could just return right there instead of break.
Stated right here
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
post #14 of 15
The code you implemented always takes O(n^2). Using the sort method, you can achieve O(n) in the best case and O(n^2) in the worst case.


A faster method for larger arrays:
  1. Copy array1 into new array2 with an index of the original order.
  2. Sort array.
  3. Mark the first value in array2 as unique and store value in a variable
  4. Compare variable to next value in array
  5. If next value is the same, continue.
  6. If next value is different, mark as unique and store value in a variable
  7. Once done, traverse array for unique values into new array
  8. Sort new array by index

Edited by DuckieHo - 2/9/11 at 6:57pm
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #15 of 15
Thread Starter 
Quote:
Originally Posted by mykah89 View Post
Congrats, sorry i didn't see this before would have given you a hand. You can probably just return true, or return false instead of declaring a boolean value in the method. Makes it a look a little cleaner in my opinion.
No problem. Thanks for your help
Quote:
Originally Posted by CovertCover View Post
Stated right here
Okay!
Quote:
Originally Posted by DuckieHo View Post
The code you implemented always takes O(n^2). Using the sort method, you can achieve O(n) in the best case and O(n^2) in the worst case.


A faster method for larger arrays:
  1. Copy array1 into new array2 with an index of the original order.
  2. Sort array.
  3. Mark the first value in array2 as unique and store value in a variable
  4. Compare variable to next value in array
  5. If next value is the same, continue.
  6. If next value is different, mark as unique and store value in a variable
  7. Once done, traverse array for unique values into new array
  8. Sort new array by index
That's impressive!
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 4200+  ASUS M2N-SLI Deluxe eVGA Geforce 7900GT CO Corsair XMS2 4GB PC2-6400 
Hard DriveOptical DriveOSMonitor
Seagate 160GB SATA2 + Western Digital 250GB SATA2 Lite-On SATA DVD Burner Linux Mint 14 Cinnamon x64   
PowerCase
OCZ GameXStream 700w Antec P180 
  hide details  
Reply
Main System
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon 64 X2 4200+  ASUS M2N-SLI Deluxe eVGA Geforce 7900GT CO Corsair XMS2 4GB PC2-6400 
Hard DriveOptical DriveOSMonitor
Seagate 160GB SATA2 + Western Digital 250GB SATA2 Lite-On SATA DVD Burner Linux Mint 14 Cinnamon x64   
PowerCase
OCZ GameXStream 700w Antec P180 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java method that determines duplicates in array