Friday, May 29, 2009

program implementing a hash table in java and giving those nos in the output which occur odd no of times in the array

/program implementing a hash table in java and giving those nos in the output which occur odd no of
//times in the array
package p1;

import java.util.*;

public class TableImp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number[]={3,3,4,4,5,6,7,7,7,3};
int key=0;
int value=0,i=0;
int ans=0;
Hashtable table = new Hashtable();
boolean flag;



while(i {
flag = table.containsKey(number[i]);
if (!flag)
{
table.put(number[i],1);

}
else
{
value=table.get(number[i]);
value++;
table.put(number[i],value);

}
i++;
}

java.util.Enumeration keys = table.keys();
while(keys.hasMoreElements())
{
Object tablekey=keys.nextElement();
Object avalue=table.get(tablekey);

value=Integer.valueOf(avalue.toString());
key=Integer.valueOf(tablekey.toString());
ans=value%2;
if (ans==1)
{
System.out.println(key);
}

}


}
}

No comments: