Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/633490689846853632
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
deleted 71 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I created a method for finding the most common character in a string (using hash-map) HashMap):

public static char getMaxViaHashmap ( String s) {     
         HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 
         char maxappearchar = ' ';
         for (int i = 0; i < s.length(); i++)
         {
              if ( map.containsKey(s.charAt(i)))
              {
                    map.put (s.charAt(i), map.get(s.charAt(i)) + 1 );
              }       
              else
              {
                map.put (s.charAt(i), 1);
              }           
         }  
         
        
          int maxValueInMap=(Collections.max(map.values()));  // This will return max value in the Hashmap    
            for (Entry<Character, Integer> entry : map.entrySet())
            {  
                if (entry.getValue()==maxValueInMap)
                {
                    System.out.println("the max char is : " + entry.getKey() + "  and displayed  " +maxValueInMap+ "  times");     // Print the key with max value
                    maxappearchar = entry.getKey();
                }
            
            }
            return maxappearchar;
    }

I am primarily concerned withQuestions:

  1. What is the performance cost (in terms of O()\$O()\$)?
  2. What is the memory cost (in terms of O()\$O()\$)?

how can the code can be optimized further ?

I created a method for finding the most common character in a string (using hash-map) :

public static char getMaxViaHashmap ( String s) {     
         HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 
         char maxappearchar = ' ';
         for (int i = 0; i < s.length(); i++)
         {
              if ( map.containsKey(s.charAt(i)))
              {
                    map.put (s.charAt(i), map.get(s.charAt(i)) + 1 );
              }       
              else
              {
                map.put (s.charAt(i), 1);
              }           
         }  
         
        
          int maxValueInMap=(Collections.max(map.values()));  // This will return max value in the Hashmap    
            for (Entry<Character, Integer> entry : map.entrySet())
            {  
                if (entry.getValue()==maxValueInMap)
                {
                    System.out.println("the max char is : " + entry.getKey() + "  and displayed  " +maxValueInMap+ "  times");     // Print the key with max value
                    maxappearchar = entry.getKey();
                }
            
            }
            return maxappearchar;
    }

I am primarily concerned with:

  1. What is the performance cost (in terms of O())?
  2. What is the memory cost (in terms of O())?

how can the code can be optimized further ?

I created a method for finding the most common character in a string (using HashMap):

public static char getMaxViaHashmap ( String s) {     
         HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 
         char maxappearchar = ' ';
         for (int i = 0; i < s.length(); i++)
         {
              if ( map.containsKey(s.charAt(i)))
              {
                    map.put (s.charAt(i), map.get(s.charAt(i)) + 1 );
              }       
              else
              {
                map.put (s.charAt(i), 1);
              }           
         }  
         
        
          int maxValueInMap=(Collections.max(map.values()));  // This will return max value in the Hashmap    
            for (Entry<Character, Integer> entry : map.entrySet())
            {  
                if (entry.getValue()==maxValueInMap)
                {
                    System.out.println("the max char is : " + entry.getKey() + "  and displayed  " +maxValueInMap+ "  times");     // Print the key with max value
                    maxappearchar = entry.getKey();
                }
            
            }
            return maxappearchar;
    }

Questions:

  1. What is the performance cost (in terms of \$O()\$)?
  2. What is the memory cost (in terms of \$O()\$)?
fixed some formatting and English to better suit a Code Review request. also changed the title slightly. added a tag
Source Link
Malachi
  • 28.7k
  • 11
  • 86
  • 188

Optimization for Finding the most common character in a string with a hash map

I created a method for finding the most common character in a string (using hash-map) :

public static char getMaxViaHashmap ( String s) {     
         HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 
         char maxappearchar = ' ';
         for (int i = 0; i < s.length(); i++)
         {
              if ( map.containsKey(s.charAt(i)))
              {
                    map.put (s.charAt(i), map.get(s.charAt(i)) + 1 );
              }       
              else
              {
                map.put (s.charAt(i), 1);
              }           
         }  
         
        
          int maxValueInMap=(Collections.max(map.values()));  // This will return max value in the Hashmap    
            for (Entry<Character, Integer> entry : map.entrySet())
            {  
                if (entry.getValue()==maxValueInMap)
                {
                    System.out.println("the max char is : " + entry.getKey() + "  and displayed  " +maxValueInMap+ "  times");     // Print the key with max value
                    maxappearchar = entry.getKey();
                }
            
            }
            return maxappearchar;
    }

iI am asking aboutprimarily concerned with:

  1. What is the performance cost (in terms of O() O())?
  2. What is the memory cost (in terms of O() O())?

how can the code can be optimizeoptimized further ?

Optimization for Finding the most common character in a string

I created a method for finding the most common character in a string (using hash-map) :

public static char getMaxViaHashmap ( String s) {     
         HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 
         char maxappearchar = ' ';
         for (int i = 0; i < s.length(); i++)
         {
              if ( map.containsKey(s.charAt(i)))
              {
                    map.put (s.charAt(i), map.get(s.charAt(i)) + 1 );
              }       
              else
              {
                map.put (s.charAt(i), 1);
              }           
         }  
         
        
          int maxValueInMap=(Collections.max(map.values()));  // This will return max value in the Hashmap    
            for (Entry<Character, Integer> entry : map.entrySet())
            {  
                if (entry.getValue()==maxValueInMap)
                {
                    System.out.println("the max char is : " + entry.getKey() + "  and displayed  " +maxValueInMap+ "  times");     // Print the key with max value
                    maxappearchar = entry.getKey();
                }
            
            }
            return maxappearchar;
    }

i am asking about:

  1. What is the performance cost (in terms of O() )?
  2. What is the memory cost (in terms of O() )?

how the code can be optimize ?

Finding the most common character in a string with a hash map

I created a method for finding the most common character in a string (using hash-map) :

public static char getMaxViaHashmap ( String s) {     
         HashMap<Character, Integer> map = new HashMap<Character, Integer>(); 
         char maxappearchar = ' ';
         for (int i = 0; i < s.length(); i++)
         {
              if ( map.containsKey(s.charAt(i)))
              {
                    map.put (s.charAt(i), map.get(s.charAt(i)) + 1 );
              }       
              else
              {
                map.put (s.charAt(i), 1);
              }           
         }  
         
        
          int maxValueInMap=(Collections.max(map.values()));  // This will return max value in the Hashmap    
            for (Entry<Character, Integer> entry : map.entrySet())
            {  
                if (entry.getValue()==maxValueInMap)
                {
                    System.out.println("the max char is : " + entry.getKey() + "  and displayed  " +maxValueInMap+ "  times");     // Print the key with max value
                    maxappearchar = entry.getKey();
                }
            
            }
            return maxappearchar;
    }

I am primarily concerned with:

  1. What is the performance cost (in terms of O())?
  2. What is the memory cost (in terms of O())?

how can the code can be optimized further ?

Source Link
Roey Cohen
  • 173
  • 1
  • 1
  • 4
Loading