priyank.mundra
BAN USERclass Node{
Character c;
int count;
public Node(Character c, int count) {
this.c = c;
this.count = count;
}
public Character getC() {
return c;
}
public void setC(Character c) {
this.c = c;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
/**
* @author pmundra
*/
public class nonRepeatingNthCharacterInStream {
public static void main(String[] args) {
String s="aaabcdeeffghh";
Map<Character,Node> m= new HashMap();
List<Node> l=new LinkedList();
int i=0;
while(i<s.length()){
Character c= s.charAt(i++);
Node n= m.get(c);
if(n==null){
Node newNode=new Node(c,1);
m.put(c,newNode);
l.add(newNode);
}
else{
n.setCount( n.count+=1);
}
}
i=0;
for(Node n:l){
if(n.count==1){
System.out.println(n.c);
i++;
}
if(i==5){
System.out.println(">>>>>>>>> "+n.c);
}
}
}
}
read the array and put the elements in a HashSet.As u keep reading the array, do get for every element and if found ,remove it.Ultimately the only element which is there odd many times will be the item left in hasSet.Reason to use hashSet is the lookup wld be O(1).
- priyank.mundra January 15, 2012