Removing duplicates from an Array

Printable View

• February 21st, 2012, 04:42 PM
Rizza
Removing duplicates from an Array
I've been trying to figure this out.

Code :

```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.
• February 21st, 2012, 05:38 PM
snowguy13
Re: Removing duplicates from an Array
Check the Set interface out; I bet it will be helpful. :)