0

Write a method that returns a new array by eliminating the duplicate values in the array using this header:

public static int[] eliminateDuplicates (int[] list)

all I have so far is my main, but what I wanted to do for the other method was use a for loop to check values at each space in my array and print them if they did not equal any of the other entries. Working on it as we speak! My output is this: The distinct numbers are: [I@4554617c

first of all this is 11 characters and at max I should be printing 10.

import java.util.Scanner;

public class EliminateDuplicates
   {
      public static void main(String [] Args)
     {
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter ten whole numbers: ");
        int[] tenNumbers = new int[10];
        for (int i=0; i<10; i++)
        {
           tenNumbers[i] = input.nextInt();
        }             
        System.out.println("The distinct numbers are: " + eliminateDuplicates(tenNumbers));

     }

  public static int[] eliminateDuplicates (int[] list)
     {
        int count = 0; 
        for (int i = 0; i > list.length; i++)
           {
            for (int j = i + 1; j < list.length; j++)
              {

                 if(list[i] == list[j])
                    {
                       list[j] = -1;
                    }                       
              }      
           }


        for (int i = 0; i < list.length; i++)
           {
              if(list[i] != -1)
                 {
                    count++;
                 }

           }
        int[] array2 = new int[count]; 
        int newCount = 0;
        for (int i = 0; i < list.length; i++)
           {
              if(list[i] != -1)
                 {
                    array2[newCount] = list[i];
                 }                  
           }      

        return array2;
     }   

   }
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 4
    "The distinct numbers are: [I@4554617c" They're so distinct they're not even numbers! –  Oct 22 '14 at 21:51
  • System.out.println("The distinct numbers are: " + eliminateDuplicates(tenNumbers)); prints the reference to an int array. You want to print the contents of the array. – DucRP Oct 22 '14 at 21:53
  • 1
    If you want to output your array via println(), you should use `System.out.println(Arrays.toString(yourArray))` and import `java.util.Arrays`. – tsabsch Oct 22 '14 at 21:54
  • obviously they are not, thats why i posted on here... – Clarence Edward Hollins Oct 22 '14 at 21:54
  • you probably just need `array2[newCount++] = list[i];` (note the ++) – njzk2 Oct 22 '14 at 21:54
  • I suspect this is a homeassignment in an introductory course to computer-science, and you must do it manually? However if that's not the case, you could could iterate the arrau, add all the numbers to a `Set` (removes duplicates) and then convert the set back to an array. – Andersnk Oct 22 '14 at 21:55
  • (although this will not work anyway if the array is not sorted, obviously) – njzk2 Oct 22 '14 at 21:55
  • Also for (int i = 0; i > list.length; i++) is incorrect. Change the > sign to < sign. – DucRP Oct 22 '14 at 21:56

2 Answers2

0
import java.util.ArrayList; 
public class EliminateDuplicates {
//This is called Generics, It'll be a little later in your studies

    public static <E> ArrayList<E> eliminateDuplicates(ArrayList<E> list) {
        ArrayList<E> newList = new ArrayList<>(list.size());
        for (E aList : list) { // for (int i = 0; i <= list.lenght; i++){
            if (!newList.contains(aList)) { 
                newList.add(aList);
            }
        }
        return newList;
    }

public static void main(String[] args) {
    ArrayList<Integer> tenNumbers = new ArrayList<Integer>();
    tenNumbers.add(14);
    tenNumbers.add(24);
    tenNumbers.add(14);
    tenNumbers.add(42);
    tenNumbers.add(25);
    tenNumbers.add(24);
    tenNumbers.add(14);
    tenNumbers.add(42);
    tenNumbers.add(25);
    tenNumbers.add(24);

    ArrayList<Integer> newList = eliminateDuplicates(tenNumbers);

    System.out.print("The distinct numbers are: " + newList);
}
}
Cosmopus
  • 3
  • 3
-1
import java.util.*;

public class Question {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] arr = {1,2,3,3,4,5,6,1};
    int[] ne = new int[arr.length];
    ArrayList<Integer> already= new ArrayList();
    int i = 0;
    while(i<arr.length){
        if(!already.contains(arr[i]))
            already.add(arr[i]);
        i++;

    }
    System.out.println(already.toString());
    }

}
Wayne_oBrien
  • 67
  • 2
  • 7