1. Longest consecutive array The algo is to put all numbers in a set. Iterate on the set, check if there’s a sequence forward or backward, track count, delete set items if found, and set max count. This will help to removing the sequence from the set if found, and only visiting it once.

    let set = new Set(arr);
     
    let maxCount = 0;
     
    for(let num of set) {
    	let cnt = 1;
    	
    	if(set.has(num-1)) {
    		for(let i=num-1; set.has(i); i--) {
    			cnt++;
    			set.delete(i);
    		}
    	}
    	
    	if(set.has(num+1)) {
    		for(let i=num+1; set.has(i); i++) {
    			cnt++;
    			set.delete(i);
    		}
    	}
    	
    	maxCount = Math.max(maxCount, cnt);
    }
     
    return maxCount;
  2. Longest subarray with given k sum. Calculate the prefix sum, iterate on the prefix sum, do sumAtIndex - k, if this exists in the map, that means the subarray exists with the sum k. Save the index in the map, which will help find the longest.

    let map = new Map();
    let sum = 0;
     
    let longest = 0;
     
    for(let [i, num] of nums.enteries()) {
    	sum += num;
    	
    	if (sum == k) {
    		maxLen = Math.max(maxLen, i + 1);
    	}
    	
    	if( map.has(sum-k) ) {
    		longest = Math.max(longest, i-map.get(sum-k));
    	}
    	
    	if(!map.has(sum))
    		map.put(sum,i);
    }
     
    return longest;
  3. Group of anagrams in a array of strings In a array of strings [], return group of anagrams.

    • Simplest way is to sort the string, put in map with array, later iterate on the map to collect the groups of array.
    • Prime hashing - prime number multiplication is always unique, so assign prime numbers to each character (use bigint), calculate product, if unique put in new array, if exists put the map’s array.