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++
#include<bits/stdc++.h>
using namespace std;
void findMode( int a[], int n)
{
map< int , int > mp;
int max = 0;
int mode = 0;
for ( int i = 0; i < n; i++)
{
mp[a[i]]++;
if (mp[a[i]] >= max)
{
max = mp[a[i]];
mode = a[i];
}
cout << mode << " " ;
}
}
int main()
{
int arr[] = { 2, 7, 3, 2, 5 };
int n = sizeof (arr)/ sizeof (arr[0]);
findMode(arr, n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void findMode( int [] a, int n)
{
Map<Integer, Integer> map
= new HashMap<>();
int max = 0 ;
int mode = 0 ;
for ( int i = 0 ; i < n; i++) {
map.put(a[i],
map.getOrDefault(a[i], 0 ) + 1 );
if (map.get(a[i]) >= max) {
max = map.get(a[i]);
mode = a[i];
}
System.out.print(mode);
System.out.print( " " );
}
}
public static void main(String[] args)
{
int arr[] = { 2 , 7 , 3 , 2 , 5 };
int n = arr.length;
findMode(arr, n);
}
}
|
Python3
def findMode(a, n):
mp = {}
max = 0
mode = 0
for i in range (n):
if a[i] in mp:
mp[a[i]] + = 1
else :
mp[a[i]] = 1
if (mp[a[i]] > = max ):
max = mp[a[i]]
mode = a[i]
print (mode, end = " " )
arr = [ 2 , 7 , 3 , 2 , 5 ]
n = len (arr)
findMode(arr,n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void findMode( int [] a, int n)
{
Dictionary< int , int > map = new Dictionary< int , int >();
int max = 0;
int mode = 0;
for ( int i = 0; i < n; i++)
{
if (map.ContainsKey(a[i]))
{
map[a[i]] = map[a[i]] + 1;
}
else
{
map.Add(a[i], 1);
}
if (map[a[i]] >= max)
{
max = map[a[i]];
mode = a[i];
}
Console.Write(mode);
Console.Write( " " );
}
}
public static void Main(String[] args)
{
int [] arr = {2, 7, 3, 2, 5};
int n = arr.Length;
findMode(arr, n);
}
}
|
Javascript
<script>
function findMode(a, n)
{
var map = new Map;
var max = 0;
var mode = 0;
for ( var i = 0; i < n; i++)
{
var put =0;
if (map.get(a[i])==undefined)
put=1;
else
put = map.get(a[i])+1;
map.set(a[i],put);
if (map.get(a[i]) >= max)
{
max = map.get(a[i]);
mode = a[i];
}
document.write(mode + " " );
}
}
var arr = [2, 7, 3, 2, 5];
var n = arr.length;
findMode(arr, n);
</script>
|
Performance Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Last Updated :
19 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...