Given an array&nbsp;of intervals&nbsp;where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

&nbsp;
Example 1:

Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].


Example 2:

Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.


&nbsp;
Constraints:


	1 &lt;= intervals.length &lt;= 104
	intervals[i].length == 2
	0 &lt;= starti &lt;= endi &lt;= 104


class Solution:
    def merge(self, A: List[List[int]]) -> List[List[int]]:
	#sort array wrt its 0'th index
        A.sort(key=lambda x:x[0])
        i=0
        while i<(len(A)-1):
            if A[i][1]>=A[i+1][0]:
                A[i][1]=max(A[i+1][1],A[i][1])
                A.pop(i+1)
            else:
                i+=1
        return(A)
                
class Solution {
    public int[][] merge(int[][] intervals) {
        // sort
        // unknown size of ans = use ArrayList
        // insert from back
            // case 1 : Merging
                // start of new interval is less that end of old interval
                // new end = Math.max(new intervals end, old intervals end)
            // case 2 : Non-merging
                // seperate interval
        // convert ArrayList to array and return

        Arrays.sort(intervals, (a,b) -> a[0]-b[0]);
        ArrayList<int[]> list = new ArrayList<>();
        for(int i = 0; i < intervals.length; i++) {
            if(i == 0) {
                list.add(intervals[i]);
            } else {
                int[] prev = list.get(list.size()-1);
                int[] curr = intervals[i];
                if(curr[0] <= prev[1]) {
                    prev[1] = Math.max(curr[1], prev[1]);
                }else {
                    list.add(curr);
                }
            }
        }
        return list.toArray(new int[list.size()][2]);
    }
}
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end());
        vector<vector<int>> res;
        int i=0;
        while(i<=intervals.size()-1){
            int start=intervals[i][0];
            int end=intervals[i][1];
            while(i<intervals.size()-1 && end>=intervals[i+1][0]){
                i++;
                if(end<intervals[i][1]) end=intervals[i][1];
            }
            i++;
            res.push_back({start, end});
        }
        return res;
    }
};
/**
 * @param {number[][]} intervals
 * @return {number[][]}
 */

var merge = function(intervals) {
    // sorting the intervals array first is a general good first step
    intervals.sort((a,b) => a[0] - b[0]);
    const result = [];
    // i am using the result array as a way to compare previous and next intervals
    result.push(intervals[0])
    for (let i=1; i<intervals.length; i++) {
        const prevInterval = result[result.length-1]
        const interval = intervals[i]
        
        if (prevInterval[1] >= interval[0]) {
            // overlap detected
            const [ l, r ] = result.pop();
            // if overlap, merge intervals by taking min/max of both boundaries
            const newL = Math.min(l, interval[0])
            const newR = Math.max(r, interval[1])
            result.push([newL, newR])
        } else {
            result.push(intervals[i])
        }
    }
    return result
};
Merge Intervals