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 2 of 2

Thread: Removing duplicates from an Array

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Removing duplicates from an Array

    I've been trying to figure this out.

    package eliminatingduplicates;
     
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.Scanner;
     
    /**
     * Name: Bilal Hasan
     * Course: CS 1337
     * Section: 501
     * Date:
     * IDE: NetBeans 7.0.1
     * Operating System: Ubuntu 11.10
     */
     
    /*
     *
     * Description: To eliminate duplicates in an array
     */
    public class EliminatingDuplicates {
     
        public static void main(String[] args) throws IOException
        {
            Scanner keyboard = new Scanner (System.in);
            Random rand = new Random ();
     
            int []array = {1,2,3,4,4,3,2,2,3,4};
                    //new int [10];
     
            /*for (int i = 0; i < array.length; i++) {
                System.out.print("Enter integer " + (i+1) + ": ");
                array[i] = keyboard.nextInt();
     
            }*/
     
            Arrays.sort(array);
     
            int []duplicateNumbers = eliminateDuplicates(array);
            int[]count = new int[10];
     
            for (int i=0; i < duplicateNumbers.length-1; i++) {
     
                for (int j = i+1; j < duplicateNumbers.length; j++) {
     
                if (duplicateNumbers[i] == duplicateNumbers[j] && duplicateNumbers[i] != 0 && j != i)
                {
                    System.out.println("Duplicate numbers are: " + duplicateNumbers[i]);
                }
     
                }  
            }
     
     
        }
     
        //////////////////////////////////////////////////////
     
        public static int[] eliminateDuplicates(int []numbers)
        {
            int []duplicates = new int [10];
            int j = 0;
     
            for (int i = 0; i < numbers.length-1; i++) {
                    for (int l = i+1; l < numbers.length; l++)
                {
                    if (duplicates [i] == duplicates [l] && l != i)
                                {
                                    duplicates[j] = numbers[i];
                                    System.out.println(duplicates[j]);
                                    j++;
     
                                }
                }
            }
     
         return duplicates;   
        }//end eliminate dupe method
    }

    I definitely need to be pointed in the right direction.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Removing duplicates from an Array

    Check the Set interface out; I bet it will be helpful.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. Removing Duplicate Values From an Array
    By nicsa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2011, 07:55 PM
  2. Eliminating Duplicates in an Array
    By m2msucks in forum Collections and Generics
    Replies: 4
    Last Post: October 30th, 2011, 03:03 AM
  3. Duplicates in Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 1
    Last Post: September 15th, 2011, 06:23 PM
  4. efficient way of checking duplicates
    By starmandell in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 2nd, 2011, 06:52 PM
  5. treemap Duplicates
    By debug in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2010, 11:52 AM