Clean JAVA code with Comments


#1

public class Solution {
// DO NOT MODIFY THE LIST. IT IS READ ONLY
public int longestConsecutive(final List A) {

    //ADD ALL ELEMENTS IN HASHSET THEN FOR EACH ELEMENT CHECK FOR THE NO OF
    //CONSEQUUIVIE SEQUENCE THAT WE GET AND ALSO PUT THE INFO ABOUT THE NO //OF
    //NEXT CONSEQUTIVE NUMBER AVAILABE IN THE MAP SO THAT YOU DONT HAVE TO 
    //LOOP THROUGH IT AGAIN AND DIRECTLY FETCH THE INFO.
    
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    HashSet<Integer> set = new HashSet<Integer>();
    int size = A.size();
    set.addAll(A);
    int count;
    int maxcount=0;
    
    for( int i=0; i<size; i++){
        map.put(A.get(i), 1);
        
    }
 
    int ncopy;
    int n;
    for(int i=0; i<size; i++){
        
        count=1;
        n=A.get(i);
        ncopy = n;
        
        while(map.containsKey(n+1)){
            if(map.get(n+1)==1){
            count++;
            }
            else{
            count+=map.get(n+1);
            break;
            }
            n++;
        }
        map.put(ncopy, count);
        if(count > maxcount)
            maxcount = count;
    }
    return maxcount;
    
}

}