Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Eliminating Duplicates in an Array

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cool Eliminating Duplicates in an Array

    Write a method to eliminate the duplicate values in the array using the following method header:

    public static int[] eliminateDuplicate(int[] numbers)

    Write a test program that reads in ten integers, invokes the method, and displays the result. Here is the sample run of the program:

    Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
    The distinct numbers are: 1 2 3 6 4 5
    Here is my code:
    import java.util.*;
     
    public class EliminatingDuplicates {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter ten numbers: ");		
    		int[] numbers = new int[10];		
    		for(int i = 0; i < numbers.length; i++) {
    			int num = input.nextInt();
    		}
    		System.out.println("The distinct numbers are: "); 
    		eliminateDuplicates(numbers);			
     	}
    	public static void eliminateDuplicates(int[] numbers) {
    		for(int i = 0; i < numbers.length; i++) {
    			boolean distinct = true;
    			for(int j = 0; j < i; j++) {
    				if (numbers[i] == numbers[j]) {
    					distinct = false;
    					break;
    				}
    			if (distinct) 
    				System.out.println(numbers[i]);
    			}
    		}
    	}
    }

    My code compiles. However when I run the program, no distinct numbers show up. This is my output.

    Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
    The distinct numbers are:
    What am I doing wrong??


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Eliminating Duplicates in an Array

    I think the problem is here:
    for(int i = 0; i < numbers.length; i++) {
    			int num = input.nextInt();
    		}
    I'm guessing you're trying to fill the array "numbers" with the numbers you input... But instead you used this "int num", you probably meant putting
    numbers[i] = input.nextInt();

  3. The Following User Says Thank You to Henry518 For This Useful Post:

    m2msucks (October 23rd, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Eliminating Duplicates in an Array

    Thanks, I fixed up that part of the code. I am able to get numbers to show up after "The distinct numbers are:". However they are not correct. I think there may be something wrong with my eliminateDuplicates method but I'm not sure what it is.

  5. #4
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Eliminating Duplicates in an Array

    There is another much simpler way:
    - Iterate over the list, adding each item to a Set.
    - Since Set does not allow duplicate items, so the result is a set of unique items.

    java exception
    Last edited by hns1984; January 11th, 2012 at 06:53 PM.

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Eliminating Duplicates in an Array

    hi,
    I am also agree that Set should be used for this what duplicate you do not want just use set of that object.

    bean factory
    Last edited by abani; December 18th, 2011 at 01:58 AM.

Similar Threads

  1. Duplicates in Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 1
    Last Post: September 15th, 2011, 06:23 PM
  2. efficient way of checking duplicates
    By starmandell in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 2nd, 2011, 06:52 PM
  3. Check for duplicates before inserting into database
    By igor0203 in forum JDBC & Databases
    Replies: 1
    Last Post: December 2nd, 2010, 09:04 AM
  4. treemap Duplicates
    By debug in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2010, 11:52 AM
  5. Eliminating duplicate values
    By Harry_ in forum Collections and Generics
    Replies: 7
    Last Post: November 9th, 2009, 06:35 AM