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

post #1 of 15
Thread Starter 
How would I go about writing a method in java that determines if there are any duplicate ints within the array?

Thanks
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 #2 of 15
Thread Starter 
ttt
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 #3 of 15
Quote:
Originally Posted by AMD_RULES View Post
How would I go about writing a method in java that determines if there are any duplicate ints within the array?

Thanks
Psuedocode:

Code:
boolean areDuplicates(int[] array)
{
     for( number of elments in the array)
     {
          store current value
          search through rest of array for value
          if(found)
               return true;
     }

     repeat for all items;

     return false;
}
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 #4 of 15
Thread Starter 
Okay here's what I got, but I need to utilize the param. How can I rewrite this to use the param in the signature?
Code:
public static boolean hasDuplicates(int[] values)
    {
          int[] numbers = { 1, 2, 3, 4, 4} ; 
          boolean found = false;
          int value = 0;
          for (int i = 0; i < numbers.length; i++){
                  if(value == numbers[i]){
                      found = true;
                  }
                  value = numbers[i];
          }
          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 #5 of 15
Quote:
Originally Posted by AMD_RULES View Post
Okay here's what I got, but I need to utilize the param. How can I rewrite this to use the param in the signature?
Code:
public static boolean hasDuplicates(int[] values)
    {
          int[] numbers = values ; 
          boolean found = false;
          int value = 0;
          for (int i = 0; i < numbers.length; i++){
                  if(value == numbers[i]){
                      found = true;
                  }
                  value = numbers[i];
          }
          return found;
    }
Changes in bold, or just change the code to use values.length instead of numbers.length
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 #6 of 15
Thread Starter 
Thank you very much!
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 #7 of 15
Thread Starter 
There's just one little problem. It always returns false and I need it to return true saying there are duplicates.
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 #8 of 15
Quote:
Originally Posted by AMD_RULES View Post
There's just one little problem. It always returns false and I need it to return true saying there are duplicates.
break statement after setting it to true, and then figure out yourself why it was causing errors.
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 #9 of 15
Thread Starter 
My professor doesn't allow us to do that in that location due to coding standards...LOL
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 #10 of 15
Quote:
Originally Posted by AMD_RULES View Post
My professor doesn't allow us to do that in that location due to coding standards...LOL
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.
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
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