# Thread: Getting rid of duplicates in my Array problem

1. Member
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;
}

}
}```
Sorry that its a bit messy, but I cleaned up most of it(was really messy before). I think up until the line
Java Code:
`System.out.println("Output");`
everything is right, from there Im completely lost

Help would be greatly appreciated :D

2. Member
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

3. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8
Originally Posted by 'Rasta.
If you could help me shorten this it would be amazing!!
are you forced to use arrays? :D

4. Member
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 5-6 days lol

5. Member
Join Date
Nov 2010
Posts
4
Rep Power
0
Originally Posted by Fubarable
Think nested for loops. :)
I have no idea what you mean with that :confused:

6. 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..... :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•