查找字符数组中出现次数最多的字符

昨天杭州大搜车面试,面试官出了一道字符算法题,如下:
给定一个字符数组,例如char[] chars = { 'a', 'b', 'b', 'b', 'b', 'c', 'a', 'a', 'a'};找出数组中出现次数最多的字符,如果存在相同次数的字符,取第一次出现的字符。
一个问题的解决方案有多种,我当时说了两种(手写了第一种):

  • 利用数据结构的特性,链表保证了插入顺序,Map正是我们想要的字符与出现次数对应关系的映射,代码如下,只需遍历一次

      char[] chars = {'a', 'b', 'b', 'b', 'b', 'c', 'a', 'a', 'a'};
      Map<Character, Integer> countMap = new LinkedHashMap<>();
      Map<Character, Integer> indexMap = new LinkedHashMap<>();
      int length = chars.length;
      // 目标字符
      char target = 0;
      // 出现的最多次数
      int maxCount = 0;
      for (int index = 0; index < length; index++) {
      	char aChar = chars[index];
      	Integer value = countMap.get(aChar);
      	
      	if (value == null) {
      		// 如果已经存在某字符 maxCount 比数组剩余待遍历元素数量还多,没必要考虑它了
      		if (maxCount > length - (index + 1)) {
      			break;
      		}
      		countMap.put(aChar, 1);
      		indexMap.put(aChar, index);
      		
      	} else {
      		countMap.put(aChar, value += 1);
      		
      		if (maxCount == value) {
      			// 出现相同次数的 char,取先在数组中出现的
      			// 即谁出现的次数 + 出现的 index 小
      			// 也可以将 value 封装成含有索引和次数的对象,那样只需声明一个 map
      			if (indexMap.get(aChar) < indexMap.get(target)) {
      				target = aChar;
      			}
      		} else if (maxCount < value) {
      			maxCount = value;
      			target = aChar;
      		}
      	}		
      }
    
  • 将原数组拷贝成orderedChars然后排序,接着遍历查找orderedCharsoriginalChars,操作比较麻烦,不推荐,但是可以锻炼纯数组操作和“指针”的使用

     char[] originalChars = {'a', 'a', 'c', 'b', 'b', 'b', 'c', 'b', 'c', 'c', 'a', 'd', 'd', 'd'};
     int length = originalChars.length;
     // 拷贝原数组,并排序
     char[] orderedChars = new char[length];
     System.arraycopy(originalChars, 0, orderedChars, 0, length);
     Arrays.sort(orderedChars);
     // 最大次数 寻找的字符,给个默认值
     int maxCount = 1;
     char target = orderedChars[0];
     int headIndex = 0, tailIndex = 1, targetIndex = -1;
     for (; tailIndex < length; ) {
     	// 移动 tailIndex 的时候 headIndex 不动
     	// tailIndex++ == (length - 1) 特殊处理 orderedChars 最后几位都是同一 char 的情况
     	if (orderedChars[headIndex] != orderedChars[tailIndex] || (tailIndex++ == (length - 1))) {
     		// 临时计数器
     		int tmpCount = tailIndex - headIndex;
     		if (tmpCount < maxCount) {
     			// 已找到出现次数最多的char 即 headIndex 的上一个
     			target = orderedChars[headIndex - 1];
     			break;
     			
     		} else if (tmpCount > maxCount) {
     			maxCount = tmpCount;
     			target = orderedChars[headIndex];
    
     		} else {
     			// 如果遇到相同次数的就比较麻烦了
     			// 需要在原数组中比较谁先出现,即索引更小者
     			int tmpCurIndex = -1;
     			for (int i = 0; i < length; i++) {
     				if (originalChars[i] == target && targetIndex == -1) {
     					targetIndex = i;
     				} else if (originalChars[i] == orderedChars[headIndex] && tmpCurIndex == -1) {
     					tmpCurIndex = i;
     				}
     				if (tmpCurIndex != -1 && targetIndex != -1) {
     					if (tmpCurIndex < targetIndex) {
     						targetIndex = tmpCurIndex;
     						target = originalChars[targetIndex];
     					}
     					break;
     				}
     			}
     		}
    
     		// 在往后找的过程中,如果找到满足条件的就将 headIndex 移至tailIndex,即 headIndex = tailIndex
     		// tailIndex 继续移动,即 ++ 操作
     		headIndex = tailIndex;
     	}
     }
原文地址:https://www.cnblogs.com/eahau/p/10980445.html