3

I'm sure there is a simple answer to this because I think I have used or heard of it previously but can't seem to find it, possibly because Google doesn't understand English. Basically I need something similar or the same as an array that does not allow duplicate values (in Java).

Example: int[] example = {1,2,2,3,4,4}

would be {1,2,3,4}

Hopefully this is clear enough for a human to understand even if Google couldn't.

1
  • 2
    My vote for the excuse of the day goes to "because Google doesn't understand English" :) Commented Dec 19, 2013 at 9:52

6 Answers 6

9

You should use a Set implementation. And also an array in Java cannot behave like a set, but with a Set implementation like HashSet it is possible to manage the uniqueness of the elements within your collection.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 a Set<Integer> such as LinkedHashSet<Integer> or TreeSet<Integer>
I was always under the impression that a Set held key and value pairs, I think I am a little confused with a Map.
1

You can use HashSet. For integers it will work fine. But later if you plan to store Objects, you should override hashCode() and equals() method. Because HashSet internally uses equals() and hashcode() to check for equality.

Read More about hashcode and equals here

Comments

0

Try using Set implementations

for example :

    public static void main(String[] args) {
    Set<Integer> numbers = new HashSet<>();

    numbers.add(3);
    numbers.add(5);
    numbers.add(5);
    numbers.add(5);
    numbers.add(6);
    numbers.add(12);

    System.out.println(numbers);   
}

will produce:

[3, 5, 6, 12]

Comments

0
 import java.util.HashSet;
 import java.util.Set;
 public static void main(String[] args) {
 Set<Integer> numbers = new HashSet<Integer>();

 numbers.add(3);
 numbers.add(5);
 numbers.add(5);
 numbers.add(5);
 numbers.add(6);
 numbers.add(12);

 System.out.println(numbers);   
 }
 Take this.you can get what you want to get.

Comments

0

Try this code for removing duplicates using Hashset

 public static Integer[] removeDuplicateUsingSet(Integer[] example) {
    List<Integer> inputList = Arrays.asList(example);
    Set<Integer> inputSet = new HashSet<Integer>(inputList);
    Integer[] ints = new Integer[inputSet.size()];
    int index = 0;
    for (Integer i : inputSet) {
        ints[index++] = i;
    }
    return ints;
}

public static void main(String[] args) {

    Integer[] example = { 1, 2, 2, 3, 4, 4 };
    example = removeDuplicateUsingSet(example);
    for(int i = 0 ;i < example.length;i++){
        System.out.println(example[i]);
    }
}

Comments

0

If can Use Hash Set while adding a element it won't allow Duplicate value or else u can use like this:

 import java.util.ArrayList;
 import java.util.Arrays;
 public class NotDuplicateArray {

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Integer[] Value={1,2,2,3,3,4,5,6,6};
            ArrayList<Integer> Check=new ArrayList<Integer>();
            int count=0;
            int x = 1;

            for(int i=0;i<Value.length;i++){
                if(Check!=null){
                    if(!Check.contains(Value[i])){
                        Check.add(Value[i]);
                    }

                }else{
                    Check.add(Value[i]);
                }

            }
            for(int v:Check){
                System.out.println(v);
            }

        }
    }

1 Comment

Output is [1,2,3,4,5,6]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.