0

I am try to remove duplicates from an array that entered by the user, by using loop and scanner only. I am trying without using any library methods, when I enter an array = {1 , 2 , 1}; the program print 1 three times.

import java.util.*;

public class Duplicates {
public static void main(String []args) {
    Scanner kb = new Scanner(System.in);

    // The size
    System.out.print("Enter the size of the array: ");
    int n = kb.nextInt();

    // the elements
    System.out.printf("Enter %d elements in the array: ", n);
    int [] a = new int[n];
    for(int i = 0; i < a.length; i++)
        a[i] = kb.nextInt();

    // remove duplicate elements
    for(int i = 0; i < a.length; i++){
        for(int j = i+1; j < a.length; j++){
            if (a[j] != a[i]){
                a[j] = a[i];
                ++j;
            }
            a[j] = a[i];
        }
    }

    // print
    for(int k = 0; k < a.length; k++)
        System.out.print(a[k] + " ");

}
}

thank you,

user3435095
  • 45
  • 2
  • 9
  • 2
    Did you google your question title first? http://stackoverflow.com/questions/10056729/java-remove-duplicates-from-an-array – djechlin Apr 18 '14 at 22:51
  • When you enter a question title it gives you a list of question with similar keywords. Check those before you post. – Anubian Noob Apr 18 '14 at 22:53
  • but the answer use set .... I want the program print the answer without using set @djechlin – user3435095 Apr 18 '14 at 22:55
  • I didn't find away to remove duplicates without using set @AnubianNoob – user3435095 Apr 18 '14 at 23:00
  • @user3435095 Then include in your question that you don't want to have library methods. Stack Overflow is frequented by professional programmers who see using library methods as smarter and more effective. You should specifically state that you want to do it manually. – Anubian Noob Apr 18 '14 at 23:03
  • http://stackoverflow.com/questions/17967114/how-to-remove-duplicates-from-an-array-in-java – Anubian Noob Apr 18 '14 at 23:03

1 Answers1

0

If you use the lang library, there is a way to remove elements from an Array.

Note: The source code below has been taken from this link

Source: http://java67.blogspot.com.au/2012/12/how-to-remove-element-from-array-in-java-example.html

import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

public class RemoveObjectFromArray{

    public static void main(String args[]) {

        //let's create an array for demonstration purpose
        int[] test = new int[] { 101, 102, 103, 104, 105};

        System.out.println("Original Array : size : " + test.length );
        System.out.println("Contents : " + Arrays.toString(test));

        //let's remove or delete an element from Array using Apache Commons ArrayUtils
        test = ArrayUtils.remove(test, 2); //removing element at index 2

        //Size of array must be 1 less than original array after deleting an element
        System.out.println("Size of array after removing an element  : " + test.length);
        System.out.println("Content of Array after removing an object : "
                           + Arrays.toString(test));

    } 

}
Hayden
  • 407
  • 1
  • 6
  • 15