Results 1 to 12 of 12
  1. #1
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default 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:
    Java 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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?

  3. #3
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: Remove Duplicates in Array

    I wouldn't even know where to begin to do that?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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.

  5. #5
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default 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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Remove Duplicates in Array

    Quote Originally Posted by HSKrustofsky View Post
    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.

  7. #7
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: Remove Duplicates in Array

    That's the thing. I wouldn't know how to...

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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.

  9. #9
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: Remove Duplicates in Array

    Here it grabs 10 numbers(sumultaniously), and outputs the numbers entered:
    Java 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!

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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.

  11. #11
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: Remove Duplicates in Array

    GOT IT!!! Finally, yes!!! Here is the code I finally came up with:
    Java 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!

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

Similar Threads

  1. Remove duplicates in 2D array
    By lakshmibvaraprasad in forum New To Java
    Replies: 5
    Last Post: 07-22-2011, 08:49 PM
  2. How can i remove a value from an array?
    By Meta in forum New To Java
    Replies: 3
    Last Post: 01-30-2011, 09:31 PM
  3. Getting rid of duplicates in my Array problem
    By 'Rasta. in forum New To Java
    Replies: 6
    Last Post: 11-20-2010, 05:51 PM
  4. Duplicates in String Array
    By turnergirl24 in forum New To Java
    Replies: 1
    Last Post: 11-04-2009, 11:09 PM
  5. FInd the no. of duplicates in an array
    By singularity in forum Advanced Java
    Replies: 3
    Last Post: 09-04-2009, 09:25 AM

Posting Permissions

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