给定一个整数,找出其中出现次数最多的数字。如果两个或多个数字出现的次数相同,则返回其中最高的一个。输入整数是作为int变量给出的,而不是字符串或数组。不允许使用哈希、数组或字符串。 例子:
null
Input: x = 12234Output: The most frequent digit is 2Input: x = 1223377Output: The most frequent digit is 7Input: x = 5Output: The most frequent digit is 5Input: x = 1000Output: The most frequent digit is 0
我们强烈建议您尽量减少浏览器,并先自己尝试。 我们可以创建一个大小为10的映射,并存储所有数字的计数,但不允许使用任何数组/字符串。 这个想法很简单,我们编写了一个函数来计算给定整数中给定数字的出现次数。然后我们计算给定整数中从0到9的所有数字。每当计数变得和之前的计数更多或相同时,我们会不断更新最大计数。下面是实现。
C++
// Finds maximum occurring digit without using any array/string #include <bits/stdc++.h> using namespace std; // Simple function to count occurrences of digit d in x int countOccurrences( long int x, int d) { int count = 0; // Initialize count of digit d while (x) { // Increment count if current digit is same as d if (x%10 == d) count++; x = x/10; } return count; } // Returns the max occurring digit in x int maxOccurring( long int x) { // Handle negative number if (x < 0) x = -x; int result = 0; // Initialize result which is a digit int max_count = 1; // Initialize count of result // Traverse through all digits for ( int d=0; d<=9; d++) { // Count occurrences of current digit int count = countOccurrences(x, d); // Update max_count and result if needed if (count >= max_count) { max_count = count; result = d; } } return result; } // Driver program int main() { long int x = 1223355; cout << "Max occurring digit is " << maxOccurring(x); return 0; } |
JAVA
// Finds maximum occurring digit // without using any array/string import java.io.*; class GFG { // Simple function to count // occurrences of digit d in x static int countOccurrences( int x, int d) { // Initialize count // of digit d int count = 0 ; while (x > 0 ) { // Increment count if // current digit is // same as d if (x % 10 == d) count++; x = x / 10 ; } return count; } // Returns the max // occurring digit in x static int maxOccurring( int x) { // Handle negative number if (x < 0 ) x = -x; // Initialize result // which is a digit int result = 0 ; // Initialize count // of result int max_count = 1 ; // Traverse through // all digits for ( int d = 0 ; d <= 9 ; d++) { // Count occurrences // of current digit int count = countOccurrences(x, d); // Update max_count // and result if needed if (count >= max_count) { max_count = count; result = d; } } return result; } // Driver Code public static void main (String[] args) { int x = 1223355 ; System.out.println( "Max occurring digit is " + maxOccurring(x)); } } // This code is contributed // by akt_mit |
Python3
# Finds maximum occurring digit # without using any array/string # Simple function to count # occurrences of digit d in x def countOccurrences(x, d): count = 0 ; # Initialize count # of digit d while (x): # Increment count if current # digit is same as d if (x % 10 = = d): count + = 1 ; x = int (x / 10 ); return count; # Returns the max occurring # digit in x def maxOccurring(x): # Handle negative number if (x < 0 ): x = - x; result = 0 ; # Initialize result # which is a digit max_count = 1 ; # Initialize count # of result # Traverse through all digits for d in range ( 10 ): # Count occurrences of current digit count = countOccurrences(x, d); # Update max_count and # result if needed if (count > = max_count): max_count = count; result = d; return result; # Driver Code x = 1223355 ; print ( "Max occurring digit is" , maxOccurring(x)); # This code is contributed by mits. |
C#
// Finds maximum occurring digit // without using any array/string class GFG { // Simple function to count // occurrences of digit d in x static int countOccurrences( int x, int d) { // Initialize count // of digit d int count = 0; while (x > 0) { // Increment count if // current digit is // same as d if (x % 10 == d) count++; x = x / 10; } return count; } // Returns the max // occurring digit in x static int maxOccurring( int x) { // Handle negative number if (x < 0) x = -x; // Initialize result // which is a digit int result = 0; // Initialize count // of result int max_count = 1; // Traverse through // all digits for ( int d = 0; d <= 9; d++) { // Count occurrences // of current digit int count = countOccurrences(x, d); // Update max_count // and result if needed if (count >= max_count) { max_count = count; result = d; } } return result; } // Driver Code static void Main() { int x = 1223355; System.Console.WriteLine( "Max occurring digit is " + maxOccurring(x)); } } // This code is contributed by mits |
PHP
<?php // Finds maximum occurring digit // without using any array/string // Simple function to count // occurrences of digit d in x function countOccurrences( $x , $d ) { // Initialize count of digit d $count = 0; while ( $x ) { // Increment count if current // digit is same as d if ( $x % 10 == $d ) $count ++; $x = (int)( $x / 10); } return $count ; } // Returns the max occurring // digit in x function maxOccurring( $x ) { // Handle negative number if ( $x < 0) $x = - $x ; $result = 0; // Initialize result // which is a digit $max_count = 1; // Initialize count of result // Traverse through all digits for ( $d = 0; $d <= 9; $d ++) { // Count occurrences of // current digit $count = countOccurrences( $x , $d ); // Update max_count and result // if needed if ( $count >= $max_count ) { $max_count = $count ; $result = $d ; } } return $result ; } // Driver Code $x = 1223355; echo "Max occurring digit is " . maxOccurring( $x ); // This code is contributed by mits ?> |
Javascript
<script> // Finds maximum occurring digit // without using any array/string // Simple function to count // occurrences of digit d in x function countOccurrences(x, d) { // Initialize count // of digit d var count = 0; while (x > 0) { // Increment count if // current digit is // same as d if (x % 10 == d) count++; x = parseInt(x / 10); } return count; } // Returns the max // occurring digit in x function maxOccurring(x) { // Handle negative number if (x < 0) x = -x; // Initialize result // which is a digit var result = 0; // Initialize count // of result var max_count = 1; // Traverse through // all digits for (d = 0; d <= 9; d++) { // Count occurrences // of current digit var count = countOccurrences(x, d); // Update max_count // and result if needed if (count >= max_count) { max_count = count; result = d; } } return result; } // Driver Code var x = 1223355; document.write( "Max occurring digit is " + maxOccurring(x)); // This code contributed by shikhasingrajput </script> |
输出:
Max occurring digit is 5
时间复杂性: O(原木) 10 十)
辅助空间: O(1)
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END