好的,所以我几天前发现了这个问题,但它已经暂停,它不会让我发布任何内容。
***注意:数组中的值或顺序是完全随机的。他们也应该是消极的。
有人推荐了这个代码,并且已经翻阅了它,但是我没有看到它如何解决这个问题。如果最少出现的元素之一不在阵列的BEGINNING处,那么这不起作用。这是因为maxCount将等于array.length,结果数组将始终采用下面编写的代码中的第一个元素。
有什么方法可以解决这个问题,使用如下的简单java?没有哈希映射和诸如此类的东西。我已经考虑了一段时间但却无法想出任何东西。也许使用双数组来存储一定数量的计数?你怎么解决这个问题?有什么指导吗?
public static void main(String[] args)
{
int[] array = { 1, 2, 3, 3, 2, 2, 4, 4, 5, 4 };
int count = 0;
int maxCount = 10;
int[] results = new int[array.length];
int k = 0; // To keep index in 'results'
// Initializing 'results', so when printing, elements that -1 are not part of the result
// If your array also contains negative numbers, change '-1' to another more appropriate
for (int i = 0; i < results.length; i++) {
results[i] = -1;
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[j] == array[i]) {
count++;
}
}
if (count <= maxCount) { // <= so it admits number with the SAME number of occurrences
maxCount = count;
results[k++] = array[i]; // Add to 'results' and increase counter 'k'
}
count = 0; // Reset 'count'
}
// Printing result
for (int i : results) {
if (i != -1) {
System.out.println("Element: " + i + ", Number of occurences: " + maxCount);
}
}
}
信用:https://stackoverflow.com/users/2670792/christian 代码
我不能竖起大拇指,所以我只想在这里说,感谢所有人的回答。
答案 0 :(得分:3)
您也可以使用面向对象方法。
首先创建一个类Pair
:
class Pair {
int val;
int occ;
public Pair(int val){
this.val = val;
this.occ = 1;
}
public void increaseOcc(){
occ++;
}
@Override
public String toString(){
return this.val+"-"+this.occ;
}
}
现在这里是主要的:
public static void main(String[] args) {
int[] array = { 1,1, 2, 3, 3, 2, 2, 6, 4, 4, 4 ,0};
Arrays.sort(array);
int currentMin = Integer.MAX_VALUE;
int index = 0;
Pair[] minOcc = new Pair[array.length];
minOcc[index] = new Pair(array[0]);
for(int i = 1; i < array.length; i++){
if(array[i-1] == array[i]){
minOcc[index].increaseOcc();
} else {
currentMin = currentMin > minOcc[index].occ ? minOcc[index].occ : currentMin;
minOcc[++index] = new Pair(array[i]);
}
}
for(Pair p : minOcc){
if(p != null && p.occ == currentMin){
System.out.println(p);
}
}
}
哪个输出:
0-1
6-1
<强>解释强>
首先排序值数组。现在你遍历它。
当前值等于前一个值时,会增加此值的出现次数。否则意味着当前值不同。因此,在这种情况下,您将创建一个具有新值和一个出现的新对。
在迭代过程中,您将跟踪您看到的最小出现次数。
现在你可以遍历你的Pair数组并检查对于每个Pair,它的出现值是否等于你找到的最小出现次数。
此算法在O(nlogn)
(由于Arrays.sort
)而不是O(n²)
的前一个版本中运行。
答案 1 :(得分:2)
此算法记录到目前为止发生次数最少的值(正如它的处理),然后将所有这些值与maxCount
的值一起打印(这是具有整体最小出现次数的值。)
快速解决方法是记录每个位置的计数,然后仅打印计数等于maxCount
(我已重命名为minCount
)的计数:
public static void main(String[] args) {
int[] array = { 5, 1, 2, 2, -1, 1, 5, 4 };
int[] results = new int[array.length];
int minCount = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[j] == array[i]) {
results[i]++;
}
}
if (results[i] <= minCount) {
minCount = results[i];
}
}
for (int i = 0; i < results.length; i++) {
if (results[i] == minCount) {
System.out.println("Element: " + i + ", Number of occurences: "
+ minCount);
}
}
}
输出:
Element: 4, Number of occurences: 1
Element: 7, Number of occurences: 1
这个版本也相当简洁,并删除了一堆不必要的变量。
答案 2 :(得分:0)
这并不像Iwburks那样优雅,但我只是在玩2D阵列并想出了这个:
public static void main(String[] args)
{
int[] array = { 3, 3, 3, 2, 2, -4, 4, 5, 4 };
int count = 0;
int maxCount = Integer.MAX_VALUE;
int[][] results = new int[array.length][];
int k = 0; // To keep index in 'results'
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[j] == array[i]) {
count++;
}
}
if (count <= maxCount) {
maxCount = count;
results[k++] = new int[]{array[i], count};
}
count = 0; // Reset 'count'
}
// Printing result
for (int h = 0; h < results.length; h++) {
if (results[h] != null && results[h][1] == maxCount ) {
System.out.println("Element: " + results[h][0] + ", Number of occurences: " + maxCount);
}
}
打印
Element: -4, Number of occurences: 1
Element: 5, Number of occurences: 1
答案 3 :(得分:0)
在上面的示例中,您似乎只使用了整数。在这种情况下我会建议以下解决方案。这将找到出现次数最少的数组中的最后一个数字。我假设你也不想要面向对象的方法。
int [] array = { 5, 1, 2, 40, 2, -1, 3, 2, 5, 4, 2, 40, 2, 1, 4 };
//initialize this array to store each number and a count after it so it must be at least twice the size of the original array
int [] countArray = new int [array.length * 2];
//this placeholder is used to check off integers that have been counted already
int placeholder = Integer.MAX_VALUE;
int countArrayIndex = -2;
for(int i = 0; i < array.length; i++)
{
int currentNum = array[i];
//do not process placeholders
if(currentNum == placeholder){
continue;
}
countArrayIndex = countArrayIndex + 2;
countArray[countArrayIndex] = currentNum;
int count = 1; //we know there is at least one occurence of this number
//loop through each preceding number
for(int j = i + 1; j < array.length; j++)
{
if(currentNum == array[j])
{
count = count + 1;
//we want to make sure this number will not be counted again
array[j] = placeholder;
}
}
countArray[countArrayIndex + 1] = count;
}
//In the code below, we loop through inspecting each number and it's respected count to determine which one occurred least
//We choose Integer.MAX_VALUE because it's a number that easily indicates an error
//We did not choose -1 or 0 because these could be actual numbers in the array
int minNumber = Integer.MAX_VALUE; //actual number that occurred minimum amount of times
int minCount = Integer.MAX_VALUE; //actual amount of times the number occurred
for(int i = 0; i <= countArrayIndex; i = i + 2)
{
if(countArray[i+1] <= minCount){
minNumber = countArray[i];
minCount = countArray[i+1];
}
}
System.out.println("The number that occurred least was " + minNumber + ". It occured only " + minCount + " time(s).");