Use hash set to find if there is endless loop.

```
public boolean isHappy(int n) {
HashSet<Integer> set= new HashSet<Integer>();
int res= 0;
while(true){
int curr= n%10;
n/= 10;
res+= curr*curr;
if(n==0){
if(res==1)
return true;
else{
n= res;
res= 0;
}
if(!set.contains(n))
set.add(n);
else
return false;
}
}
}
```