英文:
Calculating the Mode
问题
以下是翻译好的内容:
public double mode() {
if (data == null) {
double mode;
mode = Double.NaN;
return mode; // 如果数组为空,则返回无效值
}
double mode = 0;
double modeCounter = 0;
for (int c = 0; c < data.length; ++c) {
int count = 0;
for (int i = 0; i < data.length; ++i) {
if (data[i] == data[c]) // 修正这里的比较,应为 data[i] == data[c]
++count;
}
if (count > modeCounter) {
modeCounter = count;
mode = data[c];
}
}
if (!(modeCounter > 1)) {
return Double.NaN;
}
return mode;
}
英文:
I am trying to write a method that will calculate the mode of a set of data and return it. If there is no mode or more than one mode (for example: if 4 & 2 returned the most amount of times) I, want the function to return Double.NaN. I was able to get the mode, but I'm having trouble making my code return Double NaN if there is more than one mode.
Here is my code!
public double mode() {
if (data == null) {
double mode;
mode = Double.NaN;
return mode; // returns no value if array is null
}
double mode = 0;
double modeCounter = 0;
for (int c = 0; c < data.length; ++c) {
int count = 0;
for (int i = 0; i < data.length; ++i) {
if (data[i] == data[i])
++count;
}
if (count > modeCounter) {
modeCounter = count;
mode = data[c];
}
}
if (!(modeCounter > 1)) {
return Double.NaN;
}
return mode;
}
答案1
得分: 0
假设您正在使用Java。
我们可以通过简单地创建一个Map
,其中键是单独的数据点,值是它们在数据数组中的对应计数,来找到众数(以及您设置的返回Double.NaN
的限制)。
使用Stream
可以编写可读性强且简洁的代码。
如果您的数组/ArrayList中每个成员的计数都已知,您可以根据您的要求使用此计数来得出结论。
以下是实现上述解释的代码:
public static Double mode(List<Double> data) {
if (data.isEmpty()) {
// 如果输入的ArrayList为空,则返回NaN
return Double.NaN;
}
Map<Double, Integer> count = new HashMap<>();
data.forEach(
i -> {
count.putIfAbsent(i, 1);
count.put(i, count.get(i) + 1);
}
);
int maxCount = count.values().stream().max(Integer::compare).get(); // 这假设数据不为空
if (count.values().stream().filter(i -> i == maxCount).count() > 1) {
// 存在多个众数
return Double.NaN;
}
// 在这里我们可以确保只有一个众数存在
return count
.entrySet()
.stream()
.filter(e -> e.getValue() == maxCount)
.findFirst() // 因为只有一个众数
.get()
.getKey();
}
英文:
I assume that you're using Java.
We can find mode (along with the restrictions that you place to return Double.NaN
) by simply creating a Map
in which keys are the individual data points and the values are their corresponding count in the data array.
Stream
s will be superuseful to write readable and succinct code.
If you have count of every member in your array/ArrayList, you can simply use this count to draw conclusions based on your requirement.
See the following code that implements the above explanation:
public static Double mode(List<Double> data) {
if (data.isEmpty()) {
// returning NaN if the input arraylist is empty
return Double.NaN;
}
Map<Double, Integer> count = new HashMap<>();
data.forEach(
i -> {
count.putIfAbsent(i, 1);
count.put(i, count.get(i) + 1);
}
);
int maxCount = count.values().stream().max(Integer::compare).get(); // this assumes that the data is not empty
if (count.values().stream().filter(i -> i == maxCount).count() > 1) {
// more than one mode present
return Double.NaN;
}
// here we'll be sure that only one mode exists
return count
.entrySet()
.stream()
.filter(e -> e.getValue() == maxCount)
.findFirst() // as we've only one mode
.get()
.getKey();
}
专注分享java语言的经验与见解,让所有开发者获益!
评论