电话面试算法题一道:找出数组中重复次数最多的元素并打印
问题不难,看你能给出更优的方案
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map.Entry;
- import commons.algorithm.sort.QuickSort;
- /**
- * 找出数组中重复次数最多的元素并打印
- *
- */
- public class Problem_3 {
- //先快速排序后循环查找 O( n*log2(n) + n )
- public static void find1(int[] arr){
- QuickSort.sort(arr);
- int max = arr[0];
- int pre=1;
- int now=1;
- for(int i=0;i<(arr.length-1);i++){
- if(arr[i]==arr[i+1])
- &n

这是一道阿里巴巴电话面试中的算法题目,要求找出数组中重复次数最多的元素。文中提供了三种解决方案:1) 先快速排序再查找,时间复杂度为O(n log2(n) + n);2) 嵌套循环查找,时间复杂度为O(n^2);3) 使用HashMap,时间复杂度为O(n)。通过对比不同方法在不同数据量下的运行时间,显示了HashMap方法的效率优势。
最低0.47元/天 解锁文章
450





