Results 1 to 7 of 7
 11202010, 04:49 PM #1Member
 Join Date
 Nov 2010
 Posts
 4
 Rep Power
 0
Getting rid of duplicates in my Array problem
Hi there,
I'm relatively new to java and I have this one little problem which is probably quite simple, but I cant seem to get my head around it and solve it.
What I am trying to do is eliminate duplicates in my array.
Basically I want the user to be able to input 5 numbers,
then I want it to print only the numbers that are not duplicates, and also
the numbers that are less than 10 or greater than 100.
Example:
the user inputs: 10, 20, 10, 20, 101
output: 10, 20, 101
another example:
the user inputs: 1, 99, 101, 235, 99
output: 99 (because 1, 101 and 235 are outside of 10 and 100 and 99 is there twice)
This is my code so far: It just detects which numbers are duplicates.. but I cant figure the rest out at all :(
Java Code:import java.util.Scanner; public class Duplicate { public static void main(String[] args) { // instance variables  replace the example below with your own Scanner input = new Scanner(System.in); int[] myArray; //declare array myArray = new int[5]; // 5 int  0,1,2,3,4 //int x = 0; int y = 1; System.out.print("\nPlease input five numbers between 10 and 100.\n"); for(int i=0; i<5; i++)//loop 5 times { myArray[i] = input.nextInt();//input the 5 values /* if(Array[i]<10  Array[i]>100)//if its between 10 and 100 continue else return. { Array[i] = 0; } */ } System.out.println("Output"); for (int i = 0; i <= myArray.length  1; i++) { for (int j = i + 1; j <= myArray.length  1; j++) { if (myArray[i] != myArray[j]) { System.out.printf("\n%d", myArray[i]); } else { return; } } }
Java Code:System.out.println("Output");
Help would be greatly appreciated :D
 11202010, 05:40 PM #2Member
 Join Date
 Nov 2010
 Posts
 4
 Rep Power
 0
I just randomly got a burst of inspiration and figured out how to do it.
However I'm not 100% happy with it because its a bit long and repetitive.
If you could help me shorten this it would be amazing!!
Java Code:import java.util.Scanner; public class Duplicate { public static void main(String[] args) { // instance variables  replace the example below with your own Scanner input = new Scanner(System.in); int[] Array; //declare array Array = new int[5]; // 5 int  0,1,2,3,4 System.out.print("Please input five numbers between 10 and 100.\n"); for(int i=0; i<5; i++)//loop 5 times { Array[i] = input.nextInt();//input the 5 values } System.out.print("Output:"); if(Array[0]>=10 && Array[0]<=100) { System.out.printf("\n%d", Array[0]); } if(Array[1]>=10 && Array[1]<=100) { if(Array[1]!=Array[0]) { System.out.printf("\n%d", Array[1]); } } if(Array[2]>=10 && Array[2]<=100) { if(Array[2]!=Array[0] && Array[2]!=Array[1]) { System.out.printf("\n%d", Array[2]); } } if(Array[3]>=10 && Array[3]<=100) { if(Array[3]!=Array[0] && Array[3]!=Array[1] && Array[3]!=Array[2]) { System.out.printf("\n%d", Array[3]); } } if(Array[4]>=10 && Array[4]<=100) { if(Array[4]!=Array[0] && Array[4]!=Array[1] && Array[4]!=Array[2] && Array[4]!=Array[3]) { System.out.printf("\n%d", Array[4]); } } System.out.println("\nWin!!\n"); } }
Rasta

 11202010, 05:49 PM #4Senior Member
 Join Date
 Oct 2010
 Location
 Germany
 Posts
 787
 Rep Power
 7
 11202010, 05:50 PM #5Member
 Join Date
 Nov 2010
 Posts
 4
 Rep Power
 0
Yes, cause I haven't done much else in java and I would love to get the 'perfect' code because I've been working on it like mad for 56 days lol
 11202010, 05:56 PM #6Member
 Join Date
 Nov 2010
 Posts
 4
 Rep Power
 0

Think of how you'd do it on paper without a computer. One way is to go forward through the array looking to see if the current array item and has duplicates before it, and if not, print it.
So one loop will go forward, looking at each array item, and the inner loop will go backwards from the current item  1 down to 0 looking for duplicates..... :)
Similar Threads

Duplicates in more than two sets
By JavaJ in forum New To JavaReplies: 8Last Post: 12032009, 05:07 PM 
Duplicates in String Array
By turnergirl24 in forum New To JavaReplies: 1Last Post: 11052009, 12:09 AM 
FInd the no. of duplicates in an array
By singularity in forum Advanced JavaReplies: 3Last Post: 09042009, 09:25 AM 
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05152008, 10:15 AM 
Duplicates
By Gambit17 in forum New To JavaReplies: 5Last Post: 11082007, 10:56 AM
Bookmarks