I am not familiar with Java, but I have a question about a Java code which is supposed to find the first non-repeat element in an array.

Hashtable<Integer, Boolean> positions = new Hashtable<Integer,Boolean>();
      int[] elements = {2, 2, 4, 5, 1, 6, 0, 9, 1, 4, 5, 10};
      for(int i=0; i<elements.length; i++) {
          if(positions.containsKey(elements[i])) {
              if(positions.get(elements[i]).booleanValue() == false) {
                  positions.put(elements[i],true);
              }
          }
          else {
              positions.put(elements[i], false);
          }
      }
      for(int j=0; j<elements.length; j++) {
          if (positions.get(elements[j]).booleanValue() == false) {
              System.out.println("First unique element is: " + elements[j] + " at position " + j);
              break;
          }
      }

For the first 2 in the array, we put 2 and false, for the second we will put 2 and true.
My question is will the 2 and true overwrite the 2 and false or they both exist in the Hashtable? thanks

Recommended Answers

All 2 Replies

My question is will the 2 and true overwrite the 2 and false or they both exist in the Hashtable?

The value would be overwritten/updated for multiple `put`s done on the same `key`.

will the 2 and true overwrite the 2 and false or they both exist in the Hashtable? thanks

Can you write a simple program to test what happens? Then you'll know if and how it works.

Let us know what happens.

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.