2017-11-11 46 views
-1
public class Array_Duplicate { 
    public static void main(String[] args){ 
     int[] arr={2,4,2,2,6,3}; 
     int count=0; 
     for(int i=0;i<arr.length;i++){ 
      for(int j=i+1;j<arr.length;j++){ 
       if(arr[i]== arr[j]){ 
        count++; 
        System.out.println(arr[i]+" Repeated "+count+" Times"); 
       } 
      } 
     } 
    } 
} 

样本输出频率是印刷3次,但我希望它打印1次

2 Repeated 1 Times 
2 Repeated 2 Times 
2 Repeated 3 Times 

所需的输出

2 Repeated 3 times 

我怎样才能做到这一点?

+0

你是不是在你最初的方法使用任何类库,并接受它采用最类库和Java 8答案!太奇怪了!! 顺便说一句,使用'Map'将会绰绰有余,并且你的问题会变得非常初学者,看起来不像读他的书! – Yahya

回答

0

商店在地图上的计数:

public class Array_Duplicate { 
    public static void main(String[] args){ 
    int[] arr={2,4,2,2,6,3}; 
    Map<Integer, Integer> counts = new LinkedHashMap<>(); 
    for(int i=0;i<arr.length;i++){ 
     int value = arr[i]; 
     if (counts.containsKey(value)) { 
     counts.put(arr[i], counts.get(value) + 1); 
     } else { 
     counts.put(value, 1); 
     } 
    } 

    for (Integer value : counts.keySet()) { 
     int count = counts.get(value); 
     if (count > 1) { 
     System.out.println(value +" Repeated "+ count +" Times"); 
     } 
    } 
    } 
} 

与Java8流API类似的方法:

public class Array_Duplicate { 
    public static void main(String[] args){ 
    int[] arr={2,4,2,2,6,3}; 
    Arrays.stream(arr) 
     .boxed() 
     .collect(Collectors.groupingBy(i -> i, Collectors.summingInt(i -> 1))) 
     .entrySet().stream() 
     .filter(c -> c.getValue() > 1) 
     .forEach(c -> System.out.println(c.getKey() +" Repeated "+ c.getValue() +" Times")); 
    } 
} 
-1

简单。 内部循环终止后保持显示语句。

像这样的事情

public class Array_Duplicate { 
public static void main(String[] args){ 
    int[] arr={2,4,2,2,6,3}; 
    int count=0; 
    for(int i=0;i<arr.length;i++){ 
     for(int j=i+1;j<arr.length;j++){ 
      if(arr[i]== arr[j]){ 
       count++; 
      } 
     } 
     System.out.println(arr[i]+" Repeated "+count+" Times"); 
    } 
    } 
} 
+0

因此将其分配给外部变量以临时保存 – Airhead

0

如果你不想使用任何类库(坦白地说,这在Java中并不常见),你可以这样做mething这样的:

public class ArrayDuplicate { 

    public static void main(String[] args){ 
     int[] theArray = {2,4,2,2,6,3}; 
     int[] arr = theArray; // if you don't want the original array, you don't need to make a copy of it 
     while(arr.length>0){ 
      int theCount = count(arr); 
      System.out.println(arr[0]+" Repeated "+ theCount +" Times"); 
      arr = updatedArray(arr, arr[0], arr.length-theCount); 
     } 
    } 

    static int count(int[] arr){ 
    int counter=0, value=arr[0]; 
    for(int i=0;i<arr.length;i++){ 
     if(value == arr[i]){ 
      counter++; 
     } 
    } 
    return counter; 
    } 

    static int[] updatedArray(int[]arr, int value, int newLength){ 
    int []updatedArr = new int[newLength]; 
    int counter = 0; 
    for(int i=0;i<arr.length;i++){ 
     if(arr[i]!=value){ 
     updatedArr[counter++]=arr[i]; 

     } 
    } 
    return updatedArr; 
    } 
} 

输出:

2 Repeated 3 Times 
4 Repeated 1 Times 
6 Repeated 1 Times 
3 Repeated 1 Times