# 算法设计：整数流中的模式(运行整数)

2021年4月24日12:59:33 发表评论 699 次浏览

## 本文概述

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

## C ++

``````//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;

//elements one by one
for ( int i = 0; i <n; i++)
{

//that element
mp[a[i]]++;

//Checks for maximum Number
//of occurrence
if (mp[a[i]]>= max)
{

max = mp[a[i]];

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``````

## Java

``````//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 ;

//elements one by one
for ( int i = 0 ; i <n; i++) {

//that element
map.put(a[i], map.getOrDefault(a[i], 0 ) + 1 );

//Checks for maximum Number
//of occurrence
if (map.get(a[i])>= max) {

max = map.get(a[i]);

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);
}
}``````

## Python3

``````# 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 ):

# frequency
max = mp[a[i]]

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``````

## C#

``````//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;

//elements one by one
for ( int i = 0; i <n; i++)
{

//that element
if (map.ContainsKey(a[i]))
{
map[a[i]] = map[a[i]] + 1;
}
else
{
}

//Checks for maximum Number
//of occurrence
if (map[a[i]]>= max)
{

max = map[a[i]];

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``````

``2 7 3 2 2``

• 时间复杂度：O(n)
• 辅助空间：O(n)