Find the 2nd largest integer in array
If the array has no 2nd largest integer then return nil.
Reject all non integers elements and then find the 2nd largest integer in array

find_2nd_largest([1,2,3]) => 2

find_2nd_largest([1,1,1,1,1]) => nil
because all elements are same. Largest no. is 1. and there is no 2nd largest no.

find_2nd_largest([1,'a','2',3,3,4,5,'b']) => 4
as after rejecting non integers array will be [1,3,3,4,5]
Largest no. is 5. and 2nd largest is 4.

Return nil if there is no 2nd largest integer.
Take care of big numbers as well

Difficulty: introductory

Starter code:

def find_2nd_largest(arr):
	

Solutions:
def find_2nd_largest(arr):
    arr = sorted(i for i in set(arr) if type(i) == int)
    return arr[-2] if len(arr) > 1 else None

def find_2nd_largest(arr): 
    newarr = sorted(list({x for x in arr if type(x) == int})) # remove duplicates and non-int objects, sort
    return newarr[-2] if len(newarr) > 1 else None

def find_2nd_largest(arr):
    a, b = float('-inf'), float('-inf')
    
    for n in arr:
        if isinstance(n, int):
            if n > b:
                b = n
            if b > a:
                a, b = b, a
            
    return None if a == b else b

from heapq import nlargest

def find_2nd_largest(arr):
    two = nlargest(2, {v for v in arr if isinstance(v,int)})
    if len(two)==2: return two[1]

def find_2nd_largest(arr):
    return next(iter(sorted(n for n in set(arr) if type(n) == int)[-2:-1]), None)

def find_2nd_largest(arr):
    res = [arr[i] for i in range(0, len(arr)) if isinstance(arr[i], int)]
    res.sort(reverse = True)
    if len(set(arr)) == 1:
        return None
    else:
        return res[1]

    



def find_2nd_largest(arr):
    SECOND_lARGEST = -2
    arr_set = set(arr)
    numbers = sorted(number for number in arr_set if type(number) == int)
    return numbers[SECOND_lARGEST] if len(numbers) > 1 else None

def find_2nd_largest(arr):
    from collections import defaultdict
    d = defaultdict(list)
    for elem in arr:
        d[type(elem)].append(elem)
    numbers = d[int]
    largest_num = 0
    second_largest_num = 0
    for number in numbers:
        if largest_num < number:
            largest_num = number
    for number in numbers:
        if second_largest_num < number and number < largest_num:
            second_largest_num = number
    if second_largest_num == 0:
        return None
    else:
        return second_largest_num

def find_2nd_largest(arr):
    xs = sorted({x for x in arr if isinstance(x, int)}, reverse=True)
    if len(xs) > 1 and xs[0] != xs[1]:
        return xs[1]


def filter_int(arr):
    res = []
    for i in arr:
        if type(i) == int:
            res.append(i)
        else:
            continue
    return res

def sort(arr):
       for i in range(len(arr)):
           for j in range(len(arr)-1):
               if arr[j]>arr[j+1]:
                   arr[j],arr[j+1]=arr[j+1],arr[j]
       return arr


def is_diff(arr):
    count=0
    res=None
    for i in range(len(arr)-1):
        if arr[i] != arr[i+1]:
            count+=1
            res=True
            break
        else:
            res = False
    return res



def sec_big(a,b):
    if a>b:
        return b
    else:
        return a

def find_2nd_largest(arr):
    filtered_list = filter_int(arr)
    sorted_list=sort(filtered_list)
    if is_diff(sorted_list)==True:
        a=sorted_list[len(sorted_list)-1]
        b=sorted_list[len(sorted_list)-2]
        sec_largest_elem=sec_big(a,b)
        return sec_largest_elem
    else:
        return None