Remove Duplicates in Array
I need to write a program that asks the user to inter 10 numbers, removed the duplicates, and outputs the distinct numbers. After doing some Googling, this is what I could come up with and no luck:
Code:
import javax.swing.JOptionPane;
public class DistinctNumbers {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter ten numbers:");
String[] numbers = input.split(" ");
int[] numArray = new int[10];
for(int i = 0; i < numArray.length; i++) {
numArray[i] = Integer.parseInt(numbers[i]);
boolean found = false;
for(int j = 0; j < 10; j++) {
if(numArray[i] == numArray[j]) {
found = true;
}
if(!found) {
System.out.print(numArray[i] + " ");
}
}
}
}
}
If for example I enter: 1 2 3 4 5 1 2 3 4 5, the output comes out: 2 3 3 4 4 4 5 5 5 5 2 3 3 4 4 4 5 5 5 5
Any suggestions/help is greatly appreciated. Thanks in advance.
Re: Remove Duplicates in Array
Instead of trying to remove duplicates why not only add a value to the array if it is not already in the array?
Re: Remove Duplicates in Array
I wouldn't even know where to begin to do that?
Re: Remove Duplicates in Array
Yes you do. Have already written most of it. The inner loop is searching the array to find if a number is already in it and setting a boolean to true/false based on that.
As to your code in its present state, you have 2 distinct tasks: get user to enter 10 numbers & print out unique numbers. Your problem is that you are trying to do both tasks at the same time. Make your job easier and break it down into 2 tasks.
Re: Remove Duplicates in Array
I've probably read your last post at least 100 times, and I have been trying to picture exactly what to do. I go back and forth between your post and my code, and I can't seem to get exactly what I'm supposed to do.
I know you're trying to help me out without explicitly giving me the code, but is there anyway you can give another(bigger) hint?
Re: Remove Duplicates in Array
Quote:
Originally Posted by
HSKrustofsky
I've probably read your last post at least 100 times, and I have been trying to picture exactly what to do. I go back and forth between your post and my code, and I can't seem to get exactly what I'm supposed to do.
I know you're trying to help me out without explicitly giving me the code, but is there anyway you can give another(bigger) hint?
It's hard to know exactly where your stuck. To better help you and us, consider trying to implementing his suggestions even if it doesn't work, and then if it doesn't work, post your best attempt here with complete error messages.
Luck.
Re: Remove Duplicates in Array
That's the thing. I wouldn't know how to...
Re: Remove Duplicates in Array
As I said in an earlier post you have 2 tasks. Work on them separately. First thing to do is to write a program that prompts a user to enter 10 numbers and that is all. Once you have done that compiled and run your code a bunch of times to ensure that it works move onto the next stage. Which could be simply to print those 10 numbers back to the screen. Again compile and test your code several times to make sure it works. Finally add the functionality to detect duplicates and only print the unique numbers back to the screen.
Re: Remove Duplicates in Array
Here it grabs 10 numbers(sumultaniously), and outputs the numbers entered:
Code:
import javax.swing.JOptionPane;
public class DistinctNumbers {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter ten numbers:");
String[] numbers = input.split(" ");
int[] numArray = new int[10];
System.out.println("Numbers entered:");
for(int i = 0; i < numArray.length; i++) {
numArray[i] = Integer.parseInt(numbers[i]);
System.out.print(numArray[i] + " ");
}
}
}
Now, I'm lost. Do I create another class, method? I've never been good when attempting to create a method and return the array from the method. I don't know... Just completely lost!
Re: Remove Duplicates in Array
Personally I would use several methods to achieve this but you can continue in the main method. Just add more code after what you already have to detect duplicates. As I said the inner loop you had in your first post was on the right track for detecting duplicates. You just need to tweak it to do what you want.
Re: Remove Duplicates in Array
GOT IT!!! Finally, yes!!! Here is the code I finally came up with:
Code:
import javax.swing.JOptionPane;
public class DistinctNumbers {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter ten numbers:");
String[] numbers = input.split(" ");
int[] numArray = new int[10];
System.out.println("Numbers entered:");
for(int i = 0; i < numArray.length; i++) {
numArray[i] = Integer.parseInt(numbers[i]);
System.out.print(numArray[i] + " ");
}
System.out.println("\n");
System.out.println("Distinct numbers:");
for(int i = 0; i < numArray.length; i++) {
numArray[i] = Integer.parseInt(numbers[i]);
boolean found = false;
for(int j = 0; j < i; j++) {
if(numArray[i] == numArray[j]) {
found = true;
}
}
if(!found) {
System.out.print(numArray[i] + " ");
}
}
}
}
Don't know if it's best programming practice, but it works. I just have to say thanks for the help, and thanks for not bending when I felt I was asking for code solutions. Best practice to learn that way. And especially thanks for the patience. I'm new to this, and it's taking me a bit more to learn this language than I though.
THANK YOU!
Re: Remove Duplicates in Array