Banana-Leopard/TASTY huggingface dataset:
# Python3 program to find the most
# frequent element in an array.
import math as mt
 
def mostFrequent(arr, n):
 
    # Insert all elements in Hash.
    Hash = dict()
    for i in range(n):
        if arr[i] in Hash.keys():
            Hash[arr[i]] += 1
        else:
            Hash[arr[i]] = 1
 
    # find the max frequency
    max_count = 0
    res = -1
    for i in Hash:
        if (max_count < Hash[i]):
            res = i
            max_count = Hash[i]
         
    return res
 
# Driver Code
arr = [ 40,50,30,40,50,30,30]
n = len(arr)
print(mostFrequent(arr, n))
 
# This code is contributed
# by Mohit kumar 29

Time complexity: linear
Space complexity: linear
def maxFreq(arr, n):
     
    # Using moore's voting algorithm
    res = 0
    count = 1
     
    for i in range(1, n):
        if (arr[i] == arr[res]):
            count += 1
        else:
            count -= 1
         
        if (count == 0):
            res = i
            count = 1
         
    return arr[res]
 
# Driver code
arr = [ 40, 50, 30, 40, 50, 30, 30 ]
n = len(arr)
freq =  maxFreq(arr, n)
count = 0
 
for i in range (n):
        if(arr[i] == freq):
            count += 1
         
print("Element ", maxFreq(arr , n),
      " occurs ", count, " times")
 
# This code is contributed by shivanisinghss2110

Time complexity: linear
Space complexity: constant
# Python3 implementation to
# find first element
# occurring k times
 
# function to find the
# first element occurring
# k number of times
def firstElement(arr, n, k):
 
    # dictionary to count
    # occurrences of
    # each element
    for i in arr:
      count=0
      for j in arr:
        if i==j:
          count=count+1
      if count == k:
        return i
             
    # no element occurs k times
    return -1
 
# Driver Code
if __name__=="__main__":
 
    arr = [1, 7, 4, 3, 4, 8, 7];
    n = len(arr)
    k = 2
    print(firstElement(arr, n, k))
 
# This code is contributed by Arpit Jain

Time complexity: quadratic
Space complexity: constant
# Python3 implementation to
# find first element
# occurring k times
 
# function to find the
# first element occurring
# k number of times
def firstElement(arr, n, k):
 
    # dictionary to count
    # occurrences of
    # each element
    count_map = {};
    for i in range(0, n):
        if(arr[i] in count_map.keys()):
            count_map[arr[i]] += 1
        else:
            count_map[arr[i]] = 1
        i += 1
     
    for i in range(0, n):
         
        # if count of element == k ,
        # then it is the required
        # first element
        if (count_map[arr[i]] == k):
            return arr[i]
        i += 1
             
    # no element occurs k times
    return -1
 
# Driver Code
if __name__=="__main__":
 
    arr = [1, 7, 4, 3, 4, 8, 7];
    n = len(arr)
    k = 2
    print(firstElement(arr, n, k))
 
# This code is contributed
# by Abhishek Sharma

Time complexity: linear
Space complexity: linear
# importing counter from collections
from collections import Counter
 
# Python3 implementation to find
# first element occurring k times
# function to find the  first element
# occurring  k number of times
def firstElement(arr, n, k):
 
    # calculating frequencies using Counter
    count_map = Counter(arr)
 
    for i in range(0, n):
 
        # If count of element == k ,
        # then it is the required
        # first element
        if (count_map[arr[i]] == k):
            return arr[i]
        i += 1
 
    # No element occurs k times
    return -1
 
 
# Driver Code
if __name__ == "__main__":
 
    arr = [1, 7, 4, 3, 4, 8, 7]
    n = len(arr)
    k = 2
    print(firstElement(arr, n, k))
 
# This code is contributed by vikkycirus

Time complexity: nlogn
Space complexity: linear
# A Python3 program to find all symmetric
# pairs in a given array of pairs.
 
# Print all pairs that have
# a symmetric counterpart
def findSymPairs(arr, row):
   
    # This loop for selection of one pair
    for i in range(0, row):
       
        # This loop for searching of symmetric pair
        for j in range(i + 1, row):
           
            # Condition of symmetric pair
            if (arr[i][0] == arr[j][1] and arr[i][1] == arr[j][0]):
                print("(",arr[i][0],",",arr[i][1],")")
                 
# Driver Code
if __name__ == '__main__':
    arr = [[0 for i in range(2)]
            for i in range(5)]
    arr[0][0], arr[0][1] = 11, 20
    arr[1][0], arr[1][1] = 30, 40
    arr[2][0], arr[2][1] = 5, 10
    arr[3][0], arr[3][1] = 40, 30
    arr[4][0], arr[4][1] = 10, 5
    findSymPairs(arr, 5)
 
# This code is contributed by Arpit Jain

Time complexity: quadratic
Space complexity: constant
# A Python3 program to find all symmetric
# pairs in a given array of pairs.
 
# Print all pairs that have
# a symmetric counterpart
def findSymPairs(arr, row):
 
    # Creates an empty hashMap hM
    hM = dict()
 
    # Traverse through the given array
    for i in range(row):
         
        # First and second elements
        # of current pair
        first = arr[i][0]
        sec = arr[i][1]
 
        # If found and value in hash matches with first
        # element of this pair, we found symmetry
        if (sec in hM.keys() and hM[sec] == first):
            print("(", sec,",", first, ")")
 
        else: # Else put sec element of
              # this pair in hash
            hM[first] = sec
 
# Driver Code
if __name__ == '__main__':
    arr = [[0 for i in range(2)]
              for i in range(5)]
    arr[0][0], arr[0][1] = 11, 20
    arr[1][0], arr[1][1] = 30, 40
    arr[2][0], arr[2][1] = 5, 10
    arr[3][0], arr[3][1] = 40, 30
    arr[4][0], arr[4][1] = 10, 5
    findSymPairs(arr, 5)
 
# This code is contributed by Mohit Kumar

Time complexity: linear
Space complexity: linear
# Python 3program to find one of the repeating
# elements in a read only array
from math import sqrt
 
# Function to find one of the repeating
# elements
def findRepeatingNumber(arr, n):
     
    for i in arr:
      count = 0;
      for j in arr:
        if i == j:
          count=count+1
      if(count>1):
        return i
     
    # return -1 if no repeating element exists
    return -1
 
# Driver Code
if __name__ == '__main__':
     
    # read only array, not to be modified
    arr = [1, 1, 2, 3, 5, 4]
 
    # array of size 6(n + 1) having
    # elements between 1 and 5
    n = 5
 
    print("One of the numbers repeated in the array is:",
                             findRepeatingNumber(arr, n))
     
# This code is contributed by Arpit Jain

Time complexity: quadratic
Space complexity: constant
# Python Program to Find the top three repeated numbers
 
# User defined Pair class
import sys
 
class Pair:
    def __init__(self,first = 0,second = 0):
        self.first = first
        self.second = second
     
# Function to print top three repeated numbers
def top3Repeated(arr, n):
 
    # There should be atleast two elements
    if (n < 3):
        print("Invalid Input")
        return
 
    # Count Frequency of each element
    arr.sort()
    freq = {}
    for i in range(n):
        if (arr[i] in freq):
            freq[arr[i]] = 1 + freq[arr[i]]
        else:
            freq[arr[i]] = 1
 
    # Initialize first value of each variable
    # of Pair type is INT_MIN
    x = Pair()
    y = Pair()
    z = Pair()
    x.first = y.first = z.first = -sys.maxsize -1
 
    for curr,curr2 in freq.items():
        # If frequency of current element
          # is not zero and greater than
          # frequency of first largest element
        if (int(curr2) > x.first):
 
            # Update second and third largest
            z.first = y.first
            z.second = y.second
            y.first = x.first
            y.second = x.second
 
            # Modify values of x Number
            x.first = int((curr2))
            x.second = int((curr))
 
        # If frequency of current element is
        # not zero and frequency of current
        # element is less than frequency of
        # first largest element, but greater
        # than y element
        elif (int((curr2)) > y.first):
            # Modify values of third largest
            z.first = y.first
            z.second = y.second
 
            # Modify values of second largest
            y.first = int((curr2))
            y.second = int((curr))
 
        # If frequency of current element
        # is not zero and frequency of
        # current element is less than
        # frequency of first element and
        # second largest, but greater than
        # third largest.
        elif (int((curr2)) > z.first):
 
            # Modify values of z Number
            z.first = int((curr2))
            z.second = int((curr))
 
    print(f"Three largest elements are {x.second} {y.second} {z.second}")
 
# Driver's Code
arr = [ 3, 4, 2, 3, 16, 3, 15, 16, 15, 15, 16, 2, 3 ]
n = len(arr)
top3Repeated(arr, n)
 
# This code is contributed by shinjanpatra

Time complexity: linear
Space complexity: linear
# A simple Python 3 program to
# group multiple occurrences of
# individual array elements
 
# A simple method to group all
# occurrences of individual elements
def groupElements(arr, n):
 
    # Initialize all elements
    # as not visited
    visited = [False] * n
    for i in range(0, n):
        visited[i] = False
 
    # Traverse all elements
    for i in range(0, n):
     
        # Check if this is
        # first occurrence
        if (visited[i] == False):
         
            # If yes, print it and
            # all subsequent occurrences
            print(arr[i], end = " ")
            for j in range(i + 1, n):
             
                if (arr[i] == arr[j]):
                 
                    print(arr[i], end = " ")
                    visited[j] = True
             
# Driver Code
arr = [4, 6, 9, 2, 3,
       4, 9, 6, 10, 4]
n = len(arr)
groupElements(arr, n)
 
# This code is contributed
# by Smitha

Time complexity: quadratic
Space complexity: linear
# Python3 program to group multiple
# occurrences of individual array elements
 
# A hashing based method to group
# all occurrences of individual elements
def orderedGroup(arr):
     
    # Creates an empty hashmap
    hM = {}
 
    # Traverse the array elements, and store
    # count for every element in HashMap
    for i in range(0, len(arr)):
         
        # Increment count of elements
        # in HashMap
        hM[arr[i]] = hM.get(arr[i], 0) + 1
         
    # Traverse array again
    for i in range(0, len(arr)):
         
        # Check if this is first occurrence
        count = hM.get(arr[i], None)    
        if count != None:
             
            # If yes, then print
            # the element 'count' times
            for j in range(0, count):
                print(arr[i], end = " ")
                 
            # And remove the element from HashMap.
            del hM[arr[i]]
 
# Driver Code
if __name__ == "__main__":
     
    arr = [10, 5, 3, 10, 10, 4, 1, 3]
    orderedGroup(arr)
     
# This code is contributed by Rituraj Jain

Time complexity: linear
Space complexity: linear
# A Simple python 3 program to check
# if two sets are disjoint
 
# Returns true if set1[] and set2[] are disjoint, else false
def areDisjoint(set1, set2, m, n):
    # Take every element of set1[] and search it in set2
    for i in range(0, m):
        for j in range(0, n):
            if (set1[i] == set2[j]):
                return False
 
    # If no element of set1 is present in set2
    return True
 
 
# Driver program
set1 = [12, 34, 11, 9, 3]
set2 = [7, 2, 1, 5]
m = len(set1)
n = len(set2)
print("yes") if areDisjoint(set1, set2, m, n) else(" No")
 
# This code ia contributed by Smitha Dinesh Semwal

Time complexity: quadratic
Space complexity: constant
# A Simple Python 3 program to check
# if two sets are disjoint
 
# Returns true if set1[] and set2[]
# are disjoint, else false
def areDisjoint(set1, set2, m, n):
    # Sort the given two sets
    set1.sort()
    set2.sort()
 
    # Check for same elements 
    # using merge like process
    i = 0; j = 0
    while (i < m and j < n):
         
        if (set1[i] < set2[j]):
            i += 1
        elif (set2[j] < set1[i]):
            j += 1
        else: # if set1[i] == set2[j]
            return False
    return True
 
 
# Driver Code
set1 = [12, 34, 11, 9, 3]
set2 = [7, 2, 1, 5]
m = len(set1)
n = len(set2)
 
print("Yes") if areDisjoint(set1, set2, m, n) else print("No")
 
# This code is contributed by Smitha Dinesh Semwal

Time complexity: nlogn
Space complexity: constant
# Python3 program to
# check if two sets are
# distinct or not
# This function prints
# all distinct elements
def areDisjoint(set1, set2,
                n1, n2):
   
  # Creates an empty hashset
  myset = set([])
   
  # Traverse the first set
  # and store its elements in hash
  for i in range (n1):
    myset.add(set1[i])
     
  # Traverse the second set
  # and check if any element of it
  # is already in hash or not.
  for i in range (n2):
    if (set2[i] in myset):
      return False
  return True
 
# Driver method to test above method
if __name__ == "__main__":
   
  set1 = [10, 5, 3, 4, 6]
  set2 = [8, 7, 9, 3]
 
  n1 = len(set1)
  n2 = len(set2)
   
  if (areDisjoint(set1, set2,
                  n1, n2)):
    print ("Yes")
  else:
    print("No")
 
# This code is contributed by Chitranayal

Time complexity: linear
Space complexity: linear
# Python3 program to find Non-overlapping sum
from collections import defaultdict
 
# Function for calculating
# Non-overlapping sum of two array
def findSum(A, B, n):
 
    # Insert elements of both arrays
    Hash = defaultdict(lambda:0)
    for i in range(0, n):
        Hash[A[i]] += 1
        Hash[B[i]] += 1
 
    # calculate non-overlapped sum
    Sum = 0
    for x in Hash:
        if Hash[x] == 1:
            Sum += x
     
    return Sum
 
# Driver code
if __name__ == "__main__":
 
    A = [5, 4, 9, 2, 3]
    B = [2, 8, 7, 6, 3]
     
    # size of array
    n = len(A)
 
    # Function call
    print(findSum(A, B, n))
     
# This code is contributed
# by Rituraj Jain

Time complexity: linear
Space complexity: linear
# Python 3 simple program to find elements
# which are not present in second array
 
# Function for finding elements which
# are there in a[] but not in b[].
def findMissing(a, b, n, m):
 
    for i in range(n):
        for j in range(m):
            if (a[i] == b[j]):
                break
 
        if (j == m - 1):
            print(a[i], end = " ")
 
# Driver code
if __name__ == "__main__":
     
    a = [ 1, 2, 6, 3, 4, 5 ]
    b = [ 2, 4, 3, 1, 0 ]
    n = len(a)
    m = len(b)
    findMissing(a, b, n, m)
 
# This code is contributed
# by ChitraNayal

Time complexity: quadratic
Space complexity: constant
# Python3 efficient program to find elements
# which are not present in second array
 
# Function for finding elements which
# are there in a[] but not in b[].
def findMissing(a, b, n, m):
     
    # Store all elements of second
    # array in a hash table
    s = dict()
    for i in range(m):
        s[b[i]] = 1
 
    # Print all elements of first array
    # that are not present in hash table
    for i in range(n):
        if a[i] not in s.keys():
            print(a[i], end = " ")
 
# Driver code
a = [ 1, 2, 6, 3, 4, 5 ]
b = [ 2, 4, 3, 1, 0 ]
n = len(a)
m = len(b)
findMissing(a, b, n, m)
 
# This code is contributed by mohit kumar

Time complexity: linear
Space complexity: linear
# Python3 program to find given
# two array are equal or not
 
# Returns true if arr1[0..n-1] and
# arr2[0..m-1] contain same elements.
 
 
def areEqual(arr1, arr2, N, M):
 
    # If lengths of array are not
    # equal means array are not equal
    if (N != M):
        return False
 
    # Sort both arrays
    arr1.sort()
    arr2.sort()
 
    # Linearly compare elements
    for i in range(0, N):
        if (arr1[i] != arr2[i]):
            return False
 
    # If all elements were same.
    return True
 
 
# Driver Code
if __name__ == "__main__":
    arr1 = [3, 5, 2, 5, 2]
    arr2 = [2, 3, 5, 5, 2]
    n = len(arr1)
    m = len(arr2)
 
    if (areEqual(arr1, arr2, n, m)):
        print("Yes")
    else:
        print("No")

Time complexity: nlogn
Space complexity: constant
# Python3 program for the above approach
 
# Returns true if arr1[0..N-1] and
# arr2[0..M-1] contain same elements.
 
 
def is_arr_equal(arr1, arr2):
        # Check if the length of arrays are
    # equal or not: A Easy Logic Check
    if len(arr1) != len(arr2):
        return False
 
    # Create a dict named count to
    # store counts of each element
    count = {}
    # Store the elements of arr1
    # and their counts in the dictionary
    for i in arr1:
        if i in count:
                # Element already in dict, simply increment its count
            count[i] += 1
        else:
                # Element found for first time, initialize it with value 1.
            count[i] = 1
 
    # Traverse through arr2 and compare
    # the elements and its count with
    # the elements of arr1
    for i in arr2:
        # Return false if the element
        # is not in count or if any element
        # appears more no. of times than in arr1
        if i not in count or count[i] == 0:
            return False
        else:
                # If element is found, decrement
                # its value in the dictionary
            count[i] -= 1
    # Return true if both arr1 and
    # arr2 are equal
    return True
 
 
# Driver Code
if __name__ == "__main__":
    arr1 = [3, 5, 2, 5, 2]
    arr2 = [2, 3, 5, 5, 2]
 
    if is_arr_equal(arr1, arr2):
        print("Yes")
    else:
        print("No")

Time complexity: linear
Space complexity: linear
# Python3 code to find maximum shortest
# distance from endpoints
 
# function to find maximum shortest distance
def find_maximum(a, n, k):
     
    # stores the shortest distance of every
    # element in original array.
    b = dict()
     
    for i in range(n):
        x = a[i]
         
        # shortest distance from ends
        d = min(1 + i, n - i)
        if x not in b.keys():
            b[x] = d
        else:
 
            # if duplicates are found, b[x]
            # is replaced with minimum of the
            # previous and current position's
            # shortest distance*/
            b[x] = min(d, b[x])
     
    ans = 10**9
    for i in range(n):
        x = a[i]
         
        # similar elements ignore them
        # cause we need distinct elements
        if (x != (k - x) and (k - x) in b.keys()):        
            ans = min(max(b[x], b[k - x]), ans)
 
    return ans
 
# Driver code
a = [3, 5, 8, 6, 7]
K = 11
n = len(a)
print(find_maximum(a, n, K))
 
# This code is contributed by mohit kumar

Time complexity: linear
Space complexity: linear
# Python library for binary search
from bisect import bisect_left
 
# A sorting based C++ program to find missing
# elements from an array
 
# Print all elements of range [low, high] that
# are not present in arr[0..n-1]
 
def printMissing(arr, n, low, high):
     
    # Sort the array
    arr.sort()
     
    # Do binary search for 'low' in sorted
    # array and find index of first element
    # which either equal to or greater than
    # low.
    ptr = bisect_left(arr, low)
    index = ptr
     
    # Start from the found index and linearly
    # search every range element x after this
    # index in arr[]
    i = index
    x = low
    while (i < n and x <= high):
    # If x doesn't match with current element
    # print it
        if(arr[i] != x):
            print(x, end =" ")
 
    # If x matches, move to next element in arr[]
        else:
            i = i + 1
    # Move to next element in range [low, high]
        x = x + 1
 
    # Print range elements that are greater than the
    # last element of sorted array.
    while (x <= high):
        print(x, end =" ")
        x = x + 1
 
 
# Driver code
 
arr = [1, 3, 5, 4]
n = len(arr)
low = 1
high = 10
printMissing(arr, n, low, high);
 
# This code is contributed by YatinGupta

Time complexity: nlogn
Space complexity: linear
# An array-based Python3 program to
# find missing elements from an array
 
# Print all elements of range
# [low, high] that are not
# present in arr[0..n-1]
def printMissing(arr, n, low, high):
 
    # Create boolean list of size
    # high-low+1, each index i
    # representing whether (i+low)th
    # element found or not.
    points_of_range = [False] * (high-low+1)
     
    for i in range(n) :
        # if ith element of arr is in range
        # low to high then mark corresponding
        # index as true in array
        if ( low <= arr[i] and arr[i] <= high ) :
            points_of_range[arr[i]-low] = True
 
    # Traverse through the range
    # and print all elements  whose value
    # is false
    for x in range(high-low+1) :
        if (points_of_range[x]==False) :
            print(low+x, end = " ")
 
# Driver Code
arr = [1, 3, 5, 4]
n = len(arr)
low, high = 1, 10
printMissing(arr, n, low, high)
 
# This code is contributed
# by Shubh Bansal

Time complexity: linear
Space complexity: linear
# A hashing based Python3 program to
# find missing elements from an array
 
# Print all elements of range
# [low, high] that are not
# present in arr[0..n-1]
def printMissing(arr, n, low, high):
 
    # Insert all elements of
    # arr[] in set
    s = set(arr)
 
    # Traverse through the range
    # and print all missing elements
    for x in range(low, high + 1):
        if x not in s:
            print(x, end = ' ')
 
# Driver Code
arr = [1, 3, 5, 4]
n = len(arr)
low, high = 1, 10
printMissing(arr, n, low, high)
 
# This code is contributed
# by SamyuktaSHegde

Time complexity: linear
Space complexity: linear
# Python3 program to find the k-th
# missing element in a given sequence
 
 
# Returns k-th missing element. It returns -1 if
# no k is more than number of missing elements.
def find(a, b, k, n1, n2):
 
    # insert all elements of
    # given sequence b[].
    s = set()
     
    for i in range(n2):
        s.add(b[i])
 
    # Traverse through increasing sequence and
    # keep track of count of missing numbers.
    missing = 0
    for i in range(n1):
        if a[i] not in s:
            missing += 1
        if missing == k:
            return a[i]
    return -1
 
# Driver code
a = [0, 2, 4, 6, 8, 10, 12, 14, 15]
b = [4, 10, 6, 8, 12]
n1 = len(a)
n2 = len(b)
k = 3
print(find(a, b, k, n1, n2))
 
# This code is contributed by Shrikant13

Time complexity: linear
Space complexity: quadratic
# Python 3 program to find a pair
# with product in given array.
 
# Function to find greatest number
def findGreatest( arr , n):
 
    result = -1
    for i in range(n):
        for j in range(n - 1):
            for k in range(j + 1, n):
                if (arr[j] * arr[k] == arr[i]):
                    result = max(result, arr[i])
    return result
 
# Driver code
if __name__ == "__main__":
     
    arr = [10, 3, 5, 30, 35]
    n = len(arr)
 
    print(findGreatest(arr, n))
 
# This code is contributed by ita_c

Time complexity: cubic
Space complexity: constant
# Python3 program to find the largest product number
from math import sqrt
 
# Function to find greatest number
 
 
def findGreatest(arr, n):
 
    # Store occurrences of all elements in hash
    # array
    m = dict()
 
    for i in arr:
        m[i] = m.get(i, 0) + 1
 
    # Sort the array and traverse all elements from
    # end.
    arr = sorted(arr)
 
    for i in range(n - 1, 0, -1):
 
        # For every element, check if there is another
        # element which divides it.
        j = 0
        while(j < i and arr[j] <= sqrt(arr[i])):
 
            if (arr[i] % arr[j] == 0):
 
                result = arr[i]//arr[j]
 
                # Check if the result value exists in array
                # or not if yes the return arr[i]
                if (result != arr[j] and (result in m.keys())and m[result] > 0):
                    return arr[i]
 
                # To handle the case like arr[i] = 4 and
                # arr[j] = 2
                elif (result == arr[j] and (result in m.keys()) and m[result] > 1):
                    return arr[i]
 
            j += 1
 
    return -1
 
 
# Drivers code
arr = [10, 3, 5, 30, 35]
n = len(arr)
print(findGreatest(arr, n))
 
# This code is contributed by mohit kumar

Time complexity: nlogn
Space complexity: linear