-3

My question is how would I not allow duplicate values into an array? For example I have a program that is supposed to take five user input values that are between 10 and 100, and put them into an array. Also it is supposed to tell if the value the user is imputing currently is a duplicate to one found in the array currently. If so it is not supposed to record the value, and at the end of five inputs it is supposed to output the array with only unique values. So for example is the user input: 12, 16, 30, 30, 99, the end output would be: 12, 16, 30, 99. How would I eliminate the duplicates?

Person123
  • 1
  • 1
  • 9
    By using a `Set`. – Rohit Jain Mar 02 '15 at 22:26
  • Have you considered a HashTable? They are extremely fast and efficient at this exact task: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Hashtable.java – Evan Bechtol Mar 02 '15 at 22:27
  • I have to write the code myself, nothing like that can be used. – Person123 Mar 02 '15 at 22:28
  • 1
    If there are only 5 values, it's pretty easy to for loop it and check if the value already exists. – Bubletan Mar 02 '15 at 22:29
  • For each user entry, loop over the previous values and check if you have already seen it. – assylias Mar 02 '15 at 22:29
  • 2
    @Person123 Then you will have to check each input yourself and tell the user to input a different value if it is an invalid one. I think that is probably the point of the assignment. – RudolphEst Mar 02 '15 at 22:29
  • There are [lots](http://stackoverflow.com/questions/3951547/java-array-finding-duplicates) and [lots](http://stackoverflow.com/questions/27202325/how-to-find-duplicates-in-a-java-array-using-only-for-if-or-while) of [questions](http://stackoverflow.com/questions/5364154/detect-duplicate-values-in-primitive-java-array) already on SO about finding duplicates in arrays. Just adapt one of the existing solutions to your "check before insert" assignment. – azurefrog Mar 02 '15 at 22:46
  • For a range of 10..100, a brute force/"memory hungry" approach using an array of booleans would suffice. – Jongware Mar 02 '15 at 23:06
  • @Jongware Before using an array of booleans, consider using a [`BitSet`](http://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html). – David Conrad Mar 02 '15 at 23:17
  • @DavidConrad: OP needs to "write the code himself" -- no further details given, so for his sake, I hope that does not extend to console and disk I/O. ("I cannot use an existing Java compiler, I have to write one myself.") – Jongware Mar 02 '15 at 23:19
  • 1
    @Jongware Real Programmers™ encode the bits on the drive using a sharp, magnetized needle. – David Conrad Mar 02 '15 at 23:21
  • @Jongware I just meant that I have to write the code inside Java myself, I can't use other files to do it for me. – Person123 Mar 03 '15 at 00:04

2 Answers2

2

An easy way would consist in storing each inputs into a Set as it won't add data if already present.

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Note that the add method will return true if the set did not already contain the specified element which make it easier to log an error message if it does.

if(!yourSet.add(input))
    System.out.println("Already exist.");

If you look at the source code of the map method of HashSet 

public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

You will notice that everything is stored in an HashMap, then at each add, the return value will be true if the put call returned null, which will happen if there were no previous mapping for this key.

The PRESENT object is just a dummy value to be able to use the put method properly.

// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();

If you really can't use a Set, here how you can achieve this :

  • Define a boolean which will be a flag to mark if values are equals or not.
  • Loop over all datas of the array each time you have an input.
  • At each iteration set the flag to true only if data are equals.
  • Make sure your loop stop looping when the boolean is set to true

    for(int i = 0; !flag && i < array.length; i++)
    
  • Outside the loop only add data if flag's value is false.
  • Put all this in an other loop that read all user inputs until a sentinel value is entered.
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
1

Use a Set:

Set<Integer> aSet = new HashSet<Integer>();
aSet.add(12);
aSet.add(16);
aSet.add(30);
aSet.add(30);
aSet.add(99);
for (Integer number:aSet) {
    System.err.print(number+", ");
}
System.err.println();

This will print out 12, 16, 30, 99,

hd1
  • 33,938
  • 5
  • 80
  • 91