从数组中查找第二高的no,其中数组包含重复值

  • 本文关键字:数组 包含重 no 查找 java arrays
  • 更新时间 :
  • 英文 :

public class Second_Largest_Number_In_Array_with_Dupes {
public static void main(String[] args) {
int[] a = {6,8,2,4,3,1,5,7}; 

int temp ;  
for (int i = 0; i < a.length; i++){
for (int j = i + 1; j < a.length; j++){
if (a[i] < a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

System.out.println("Second Largest element is " + a[1]);
}
}

这只适用于没有重复值的数组。

例如,它不适用于此阵列:int[] a = {6,8,2,4,3,1,5,7,8};

除了正确的答案,还可以使用以下算法:

public static void main(String[] args) {
int [] a= {6,8,2,4,3,1,5,7,8} ;
SortedSet<Integer> sortedSet = new TreeSet<Integer>();
Arrays.stream(a).forEach(sortedSet::add);
sortedSet.remove(sortedSet.last());
System.out.println(sortedSet.last());
}

添加到已排序的集合中,使最高值位于该集合的末尾。此外,通过使用集合,我们可以删除任何重复的值。要获得最高的元素,可以执行sorted_set.last(),因为第二高的元素必须首先移除最后一个元素。

可以将函数推广到任何nth最大元素,比如:

public static int getNthMaxElement(int [] array, int nth){
SortedSet<Integer> sortedSet = new TreeSet<Integer>();
Arrays.stream(array).forEach(sortedSet::add);
if(array.length < nth) return Integer.MIN_VALUE;
for(int i = 1; i < nth; i++)
sortedSet.remove(sortedSet.last());
return sortedSet.last();
}

测试:

public static void main(String[] args) {
int [] a= {6,8,2,4,3,1,5,7,8} ;
for(int i = 1; i < a.length; i++)
System.out.println(i+"th max : "+getNthMaxElement(a, i));
}

输出:

1th max : 8
2th max : 7
3th max : 6
4th max : 5
5th max : 4
6th max : 3
7th max : 2
8th max : 1

解决方案

  • 使用尽可能低的值Integer.MIN_Value初始化max和second_max
  • 然后遍历数组并检查每个元素是否大于max_value->如果是,则使用[i]重新分配您的最大值,并使用max_malue重新分配第二个_max_值

if (a[i] > max) {
temp = max;
max = a[i];
second_max = temp;
  • 然后检查a[i]是否大于second_max_value,而不等于max_value。如果是,则使用[i]重新分配您的second_max_value
    a[i] > second_max && a[i] != max

  • 最后但同样重要的是,检查second_max是否等于初始值
    如果是,则在您的数组中没有第二高的值。此情况下的数组示例{5,5,5},{1}@感谢Henry

这里是代码

public static void main(String[] args) {
int[] a = { 6, 8, 2, 4, 3, 1, 5, 7 };
int max = Integer.MIN_VALUE;
int second_max = Integer.MIN_VALUE;
int temp;
for (int i = 0; i < a.length; i++) {
if (a[i] > max) {
temp = max;
max = a[i];
second_max = temp;
} else if (a[i] > second_max && a[i] != max) {
second_max = a[i];
}
}
if (second_max == Integer.MIN_VALUE) {
System.out.println("No Second Highest value");
} else {
System.out.println(second_max);
}
}

输出

7

您可以利用java内置集合
为此,您需要一个结构,该结构可以保留没有重复的值,还可以按顺序存储值
(例如:自然顺序,在这种情况下升序(。

import java.util.SortedSet;
import java.util.TreeSet;
public class MinArr {
public static void main(String[] args) {
int[] a = { 6, 8, 2, 4, 3, 1, 5, 7, 7, 8, 8 };
SortedSet<Integer> set = new TreeSet<Integer>();
for (int i = 0; i < a.length; i++) {
set.add(a[i]);
}
set.forEach(System.out::println);
if (set.size() >= 2) {
set.remove(set.last());
System.out.println("second max is =" + set.last());
} else {
System.out.println("there is no second max");
}
}
}

输出:

//list values
1
2
3
4
5
6
7
8
second max is =7

获得数组中k个最高数的最佳方法如果空间不是约束,那么我们可以使用优先级队列。

int[] a = {6,8,2,4,3,1,5,7}; 

PriorityQueue<Integer> pr = new PriorityQueue<Integer>(Collections.reverseOrder());
for (int i = 0; i < a.length; i++) {
pr.add(a[i]);
}
pr.poll();
System.out.println(pr.poll());``
public static void main(String[] args) {
int[] a = {6, 8, 2, 4, 3, 1, 5, 7, 8};
TreeSet<Integer> sortedSet = new TreeSet<>();
stream(a).forEach(sortedSet::add);
sortedSet.remove(sortedSet.last());
System.out.println(sortedSet.last());
}

这是我的想法,与上面的不同

public class SecondLargestNumber {
public static void main(String[] args) {

int[] nums = {1,8,8,5,7,9,9,-1,-5,1,7};

int arrsize = nums.length;

Arrays.sort(nums);

int[] sortedarry = new int[arrsize];

for(int i=0; i<arrsize; i++){
sortedarry[i] = nums[arrsize - i -1];
}

for(int i=0; i <arrsize; i++){
if(sortedarry[i] != sortedarry[i+1]){
System.out.println("Second largest number is: "+sortedarry[i+1]);
break;
}
}
}
}

***可以帮助其他人!!***

最新更新