/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testelimination3;
import java.util.Scanner;
/**
 *
 * @author Nick
 */
public class TestElim {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter a set of numbers");
       int[] numbers = new int[10];
        for(int i = 0; i < numbers.length; i++){
            numbers[i] = input.nextInt();

        }

       System.out.println("Without Duplicates: ");
    }
    public static void printArray(int[] newArray){
        for(int i = 0; i < newArray.length; i++){
            System.out.print(newArray[i] + "   ");


        }
    }







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

  int[] newArray= new int[numbers.length];
  for(int i = 0; i <numbers.length; i++ ){


  for(int j = 0; j < newArray.length; j++ )
  {
  if(newArray[j] == numbers[i]) {
          break;
      }
  }
  newArray[newArray.length] = numbers[i];
  }
  return newArray;
  }


}

Hi everyone, i am new to java coding so bear with me haha. I need to create a program that takes in an arrary and checks it for duplicates, then returns the new array without any duplicates. I haveto use a method and i cannot use hashsets or anything like that. When i run the program it doesnt work and sometimes gives me errors. What i have so far is listed below. Any help is appreciated. Thanks!!!

sometimes gives me errors

Please post the full text of the error messages.

What is the algorithm/design that you have for removing duplicates? Can you list the steps the program should take to find and remove duplicates? When you get a good algorithm, then work on coding it in java.

On line 49 you always add the new number at thelast+1 position in newArray. That's doubly wrong - each value will overwrite the previous one, except that the index value is invalid anyway. You could keep a variable that tracks how many numbers you have already added so that will tell you where to insert the next one...

Ps don't just say "gives errors", always post the complete text of any error messages because they contain essential info.

I just ran the code again and it didnt give me any errors but it still doesnt work correctly. What the program should do is, read in a set of numbers, then create and arry for it, then create another array to compare it against, then print out just the single numbers, not the duplicates.

Sorry if i am not being more clear, i am new at it and dont fully understand.

Can you explain how these steps are done? What are the steps the program must do?

1)create another array to compare it against.

What is in this second array and how are its contents put into the array?

2)print out just the single numbers, not the duplicates

How does the program find the single numbers so it can print them?

dont fully understand

That's why you need to work on what steps the program is going to take to solve the problemn BEFORE writing any code for it.

Your algorithm just is not correct;
Your inner loop is iterating through the newArray even before it has an element. This is incorrect .
Have boolean variable inside the first FOR loop and it should placed be before second FOR loop.
You need to iterate through same array list in both inner and outer "for loop".
In the inner iteration Check for the elements from both iterations that not having identical indexes . If there is a match then put the value of boolean variable as true and then break the inner loop.
Then check for the for boolean variable if it is not true , then put the value of the outer indexed element into new array.

This logic i have implemented it and it works.
Please modify the code and try as described above

I have implemented this logic

Code removed. Please don't do the OPs work.

In case still there are any improvement to this logic please let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.