+ 1
Hi guys I want to return the minimum duplicated number with this code. It's not running. What do I need to fix? JAVA..
4 Antworten
+ 5
@Ricky the double loop in main will jump out of index. Then your algorithm in minDump is looking for the minNumber, you are not counting the actual appearances of of number in the array. Make a loop on the array. Create a map and do the following: if a key equal to the current array member exists -> increase the value by one, else create the key and set the value to one. At the end search the min value in the map and output the corresponding key.
+ 2
thx Nick
+ 1
public class Program
{
public static void main(String[] args) {
int [] arr={1,1,2,2,3,4,4};
int minNum = minDup (arr);
System.out.println (minNum);
for (int i=0;i<arr.length;i++){
for (int j=i+1;i<j.length;j++) {
if ((arr [i]==(arr [j]))&&(i!=j)) {
system.out.println(arr[j]);
}
}
}
public static int minDup (int [] a){
int minNum = a[0];
for (int i = 1; i <= a.length - 1; i++) {
if (a[i]<minNum) {
minNum = a[i] ;
}
}
return minNum ;
}
}
HERE IS THE CODE
+ 1
well for one thing looks like a } is missing before your minDup function