public int findNumber(int[] array) { // Write your code here. } 找到数组中出现次数最多的次数
时间: 2024-02-27 14:52:02 浏览: 71
好的,针对您的问题,以下是一个可能的解决方案:
```
public int findNumber(int[] array) {
if (array == null || array.length == 0) {
return 0;
}
HashMap<Integer, Integer> map = new HashMap<>();
int maxCount = 0;
for (int i = 0; i < array.length; i++) {
int count = map.getOrDefault(array[i], 0) + 1;
map.put(array[i], count);
maxCount = Math.max(maxCount, count);
}
return maxCount;
}
```
这个方法的思路是使用一个 HashMap 来记录每个数字出现的次数。遍历整个数组,对于每个数字,我们在 map 中查找该数字是否已经出现过,如果出现过,将该数字的出现次数加 1;如果没有出现过,将该数字的出现次数设为 1。同时,我们还要更新一个 maxCount 变量,用来记录出现次数最多的数字的出现次数。最后返回 maxCount 即可。
需要注意的是,如果数组是空的,或者长度为 0,那么我们应该返回 0。
相关问题
public int findNumber(int[] array) { // Write your code here. } 在给定的数组中,找到出现次数最多的数字。 出现次数相同时,返回数值最小的数字
好的,针对您的问题,以下是一个可能的解决方案:
```
public int findNumber(int[] array) {
if (array == null || array.length == 0) {
return -1;
}
Map<Integer, Integer> map = new HashMap<>();
int maxCount = 0;
int minNumber = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
int count = map.getOrDefault(array[i], 0) + 1;
map.put(array[i], count);
if (count > maxCount || (count == maxCount && array[i] < minNumber)) {
maxCount = count;
minNumber = array[i];
}
}
return minNumber;
}
```
这个方法的思路与之前的类似,同样是使用一个 HashMap 来记录每个数字出现的次数。不同之处在于,在更新出现次数最多的数字时,我们需要同时记录下当前最小的数字。
具体来说,我们可以维护两个变量,`maxCount` 和 `minNumber`,分别表示出现次数最多的数字的出现次数和数值最小的数字。遍历整个数组,对于每个数字,我们在 map 中查找该数字是否已经出现过,如果出现过,将该数字的出现次数加 1;如果没有出现过,将该数字的出现次数设为 1。同时,我们还需要判断当前数字出现的次数是否比之前的最大值更大,或者是否与之前的最大值相等但是该数字比之前的最小数字更小。如果是,就更新 `maxCount` 和 `minNumber`。
需要注意的是,如果数组是空的,或者长度为 0,那么我们应该返回 -1,表示数组中没有任何数字。
Java(Algebra: perfect square) Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, and 5 in an array list. 2 and 5 appear an odd number of times in the array list. Thus, n is 10.) Here are some sample runs: Enter an integer m: 1500 The smallest number n for m * n to be a perfect square is 15 m * n is 22500
Here's the Java code to solve this problem:
```
import java.util.*;
public class PerfectSquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer m: ");
int m = input.nextInt();
List<Integer> factors = getSmallestFactors(m);
int n = 1;
for (int factor : factors) {
if (getFrequency(factors, factor) % 2 != 0) {
n *= factor;
}
}
System.out.println("The smallest number n for m * n to be a perfect square is " + n);
System.out.println("m * n is " + (m * n * n));
}
public static List<Integer> getSmallestFactors(int n) {
List<Integer> factors = new ArrayList<>();
int factor = 2;
while (n > 1) {
if (n % factor == 0) {
factors.add(factor);
n /= factor;
} else {
factor++;
}
}
return factors;
}
public static int getFrequency(List<Integer> list, int value) {
int count = 0;
for (int element : list) {
if (element == value) {
count++;
}
}
return count;
}
}
```
Explanation:
The `getSmallestFactors` method takes an integer `n` as input and returns a list of its smallest factors. It does this by repeatedly dividing `n` by its smallest factor until `n` becomes 1.
The `getFrequency` method takes a list of integers and a value as input and returns the number of times the value appears in the list.
In the `main` method, we first prompt the user to enter an integer `m`. Then we call `getSmallestFactors` to get the list of smallest factors of `m`. We then iterate through the list of factors and multiply the ones that appear an odd number of times. This gives us the smallest integer `n` such that `m * n` is a perfect square. Finally, we print out the value of `n` and `m * n * n`.
阅读全文