'Optimal algorithm to return largest k elements from an array of infinite number of elements in running stream

I have a running stream of integers, how can I take largest k elements from this stream at any point of time.



Solution 1:[1]

This problem is also called Heavy Hitters . Count Sketch Data Structures are the solution for this .

Ref :

Solution 2:[2]

import heapq
def klargestelements(arr1,k):
    q=heapq.nlargest(k,arr1)
    return q
k=3
arr1=[1,2,4,5,6,7]
m=klargestelements(arr1,k)
print(m)

nsmallest or nlargest methods takes in the argument k and the array in which the min/max elements are to be found

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user666
Solution 2 ravi tanwar