user2913664
user2913664

Reputation:

Array that does not allow duplicates (Java)

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.

Upvotes: 3

Views: 10359

Answers (6)

preethi
preethi

Reputation: 1

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);
            }

        }
    }

Upvotes: 0

Asfab
Asfab

Reputation: 386

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]);
    }
}

Upvotes: 0

user3101409
user3101409

Reputation: 1

 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.

Upvotes: 0

Aman Arora
Aman Arora

Reputation: 1252

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

Upvotes: 1

Hamid Samani
Hamid Samani

Reputation: 445

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]

Upvotes: 0

Juvanis
Juvanis

Reputation: 25950

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.

Upvotes: 9

Related Questions