Hai
Can anybody help me out to write a java program which identifies a duplicate value in a Vector
please send me the code along with the examples.
links are also welcomed
Thanks in Advance
Srinivas Kundan
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hai
Can anybody help me out to write a java program which identifies a duplicate value in a Vector
please send me the code along with the examples.
links are also welcomed
Thanks in Advance
Srinivas Kundan
Vector as in java.util.Vector<E> ?
Have a google at it, its not that hard.
// Json
Yeah pretty simple, still
AcumenVector<String> strVect = new Vector<String>();
for(String str : strVect) {
if(strVect.contains(str)) {
return true;
}
}
return false;
Lucid forums
That for loop seems redundant to me, you might as well remove that. By the way Vectors are more or less also redundant as you have ArrayList these days. Only reason for the Vector might be concurrency but then again it's not fully thread safe using Vector anyways.
// Json
Hi Json,
Appreciate you pointing that. I totally understand that I am doing in O(n^2) and we can do using hashing in O(n) space and O(n) time OR O(nlogn) time and constant space by sorting. But that is all algorithms. Looking at the question, it clearly shows the person who asked question must be new to Java, so I replied trivial solution.
In fact I think you can do it by just exchanging across the data structures in Java also I guess.
Nice to met you Json.
Acumen
Lucid forums
Nice to meet you too.
I understand what you did but as well as teaching people how to do things its also nice to comment on bad practices I believe.
// Json
Totally correct, please don't get me wrong Just explaining reason why I replied that way. Feedback of any kind is always welcome, that is the way we grow as people
Agree
// Json
whenever you try to add an element to this hash table, it will return false if a duplicate is found. Note that a duplicate is compared by .equals method of Object. Should be almost O(n) running time, unless there are large clusters.
public class HashTable<E> { int size; ArrayList<ArrayList<E>> hash; /** * Creates an empty hash table with a given size * * @param size */ public HashTable (int size) { this.size = size; hash = new ArrayList<ArrayList<E>>(); hash.ensureCapacity(size); for (int i = 0; i < size; i++) { hash.add(new ArrayList<E>()); } } /** * Adds an element to the hash table.<br> * * @param element * @return true if successfully added, false otherwise (duplicate) */ public boolean add (E element) { if (hash.contains(element)) { return false; } hash.get(hashFunction(element.toString())).add(element); return true; } /** * Determines if an element is in the hash table. * * @param element * @return true if found, false otherwise */ public boolean contains (E element) { // get a list of those that hash to the same value ArrayList<E> list = hash.get(hashFunction(element.toString())); for (int i = 0; i < list.size(); i++) { // try to find the element from the list if (list.get(i).equals(element)) { return true; } } // didn't find element return false; } /** * Gets an element from the hash, if it exists.<br> * If duplicates (toString() matches), the first is returned. * * @return the item (null if none) */ public E get (E element) { // get a list of those that hash to the same value ArrayList<E> list = hash.get(hashFunction(element.toString())); for (int i = 0; i < list.size(); i++) { // find one who's to string matches, remove and return if (list.get(i).equals(element)) { return list.get(i); } } // didn't find any, return return null; } /** * Removes an element from the hash, if it exists.<br> * If duplicates (toString() matches), the first is removed. * * @return the removed item (null if none) */ public E remove (E element) { // get a list of those that hash to the same value ArrayList<E> list = hash.get(hashFunction(element.toString())); for (int i = 0; i < list.size(); i++) { // find one who's to string matches, remove and return if (list.get(i).equals(element)) { return list.remove(i); } } // didn't find any, return return null; } /** * @return the size of the hash table array */ public int size () { return size; } /** * Takes a string representation of an object and hashes it. * * @param str * @return */ private int hashFunction (String str) { int hashval = 0; for (int i = 0; i < str.length(); i++) { hashval = 37 * hashval + str.charAt(i); } hashval %= size; if (hashval < 0) hashval += size; return hashval; } }
JavaPF (August 12th, 2009)