Mode in a stream of integers (running integers)

Given that integers are being read from a data stream. Find the mode of all the elements read so far starting from the first integer till the last integer.

Mode is defined as the element which occurs the maximum time. If two or more elements have the same maximum frequency, then take the one with the last occurrence. 

Examples:

Input: stream[] = {2, 7, 3, 2, 5}
Output: 2 7 3 2 2 
Explanation: 
Mode of Running Stream is computed as follows: 
Mode({2}) = 2 
Mode({2, 7}) = 7 
Mode({2, 7, 3}) = 3 
Mode({2, 7, 3, 2}) = 2 
Mode({2, 7, 3, 2, 2}) = 2

Input: stream[] = {3, 5, 9, 9, 2, 3, 3, 4}
Output: 3 5 9 9 9 3 3 3



Approach: The idea is to use a Hash-map to map elements to its frequency. While reading the elements one by one update the frequencies of elements in the map and also update the mode which will be the mode of the stream of the running integers.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
#include<bits/stdc++.h>
using namespace std;
  
// Function that prints
// the Mode values
void findMode(int a[], int n)
{
      
    // Map used to mp integers
    // to its frequency
    map<int, int> mp; 
  
    // To store the maximum frequency
    int max = 0;
  
    // To store the element with
    // the maximum frequency
    int mode = 0;
      
    // Loop used to read the
    // elements one by one
    for(int i = 0; i < n; i++) 
    {
          
        // Updates the frequency of
        // that element
        mp[a[i]]++;
      
        // Checks for maximum Number
        // of occurrence
        if (mp[a[i]] >= max) 
        {
      
            // Updates the maximum frequency
            max = mp[a[i]];
      
            // Updates the Mode
            mode = a[i];
        }
        cout << mode << " ";
    }
}
      
// Driver Code
int main()
{
    int arr[] = { 2, 7, 3, 2, 5 };
    int n = sizeof(arr)/sizeof(arr[0]);
  
    // Function call
    findMode(arr, n);
  
    return 0;
  
// This code is contributed by rutvik_56

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the
// above approach
  
import java.util.*;
  
public class GFG {
  
    // Function that prints
    // the Mode values
    public static void findMode(int[] a, int n)
    {
        // Map used to map integers
        // to its frequency
        Map<Integer, Integer> map
            = new HashMap<>();
  
        // To store the maximum frequency
        int max = 0;
  
        // To store the element with
        // the maximum frequency
        int mode = 0;
  
        // Loop used to read the
        // elements one by one
        for (int i = 0; i < n; i++) {
  
            // Updates the frequency of
            // that element
            map.put(a[i],
                    map.getOrDefault(a[i], 0) + 1);
  
            // Checks for maximum Number
            // of occurrence
            if (map.get(a[i]) >= max) {
  
                // Updates the maximum frequency
                max = map.get(a[i]);
  
                // Updates the Mode
                mode = a[i];
            }
  
            System.out.print(mode);
            System.out.print(" ");
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 2, 7, 3, 2, 5 };
  
        int n = arr.length;
  
        // Function Call
        findMode(arr, n);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the 
# above approach
  
# Function that prints 
# the Mode values 
def findMode(a, n):
      
    # Map used to mp integers 
    # to its frequency 
    mp = {}
      
    # To store the maximum frequency 
    max = 0
      
    # To store the element with 
    # the maximum frequency 
    mode = 0
      
    # Loop used to read the 
    # elements one by one 
    for i in range(n):
        if a[i] in mp:
            mp[a[i]] += 1
        else:
            mp[a[i]] = 1
          
        # Checks for maximum Number 
        # of occurrence     
        if (mp[a[i]] >= max):
              
            # Updates the maximum 
            # frequency 
            max = mp[a[i]]
              
            # Updates the Mode 
            mode = a[i]
              
        print(mode, end = " ")
  
# Driver Code
arr = [ 2, 7, 3, 2, 5 ]
n = len(arr)
  
# Function call 
findMode(arr,n)
  
# This code is contributed by divyeshrabadiya07

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the
// above approach
using System;
using System.Collections.Generic;
class GFG{
  
    // Function that prints
    // the Mode values
    public static void findMode(int[] a, int n)
    {
        // Map used to map integers
        // to its frequency
        Dictionary<int, int> map = new Dictionary<int, int>();
  
        // To store the maximum frequency
        int max = 0;
  
        // To store the element with
        // the maximum frequency
        int mode = 0;
  
        // Loop used to read the
        // elements one by one
        for (int i = 0; i < n; i++) 
        {
          
            // Updates the frequency of
            // that element
            if (map.ContainsKey(a[i])) 
            {
                map[a[i]] = map[a[i]] + 1;
            }
            else
            {
                map.Add(a[i], 1);
            }
  
            // Checks for maximum Number
            // of occurrence
            if (map[a[i]] >= max) 
            {
  
                // Updates the maximum frequency
                max = map[a[i]];
  
                // Updates the Mode
                mode = a[i];
            }
            Console.Write(mode);
            Console.Write(" ");
        }
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int[] arr = {2, 7, 3, 2, 5};
        int n = arr.Length;
  
        // Function Call
        findMode(arr, n);
    }
}
  
// This code is contributed by Amit Katiyar

chevron_right


Output: 

2 7 3 2 2

Performance Analysis: 

  • Time Complexity: O(N)
  • Auxiliary Space: O(N) 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.