2013-07-13 114 views
0

我有一個整數數組crr_array,我想對重複出現的元素進行計數。首先,我讀取數組的大小並使用從控制檯讀取的數字進行初始化。在數組new_array中,我存儲了重複的元素。數組times存儲元素連續出現的次數。然後,我嘗試搜索重複序列並以特定格式打印它們。但是,它不起作用。在整數數組中計數重複的元素

// Get integer array size 
Scanner input = new Scanner(System.in); 
System.out.println("Enter array size: "); 
int size = input.nextInt(); 

int[] crr_array = new int[size]; 
int[] new_array= new int[size]; 
int[] times = new int[size]; 

// Read integers from the console 
System.out.println("Enter array elements: "); 
for (int i = 0; i < crr_array.length; i++) { 
    crr_array[i] = input.nextInt(); 
    times[i] = 1; 
} 

// Search for repeated elements 
for (int j = 0; j < crr_array.length; j++) { 
    for (int i = j; i < crr_array.length; i++) { 
     if (crr_array[j] == crr_array[i] && j != i) { 
      new_array[i] = crr_array[i]; 
      times[i]++; 
     } 
    } 
} 



//Printing output 
for (int i = 0; i < new_array.length; i++) { 
    System.out.println("\t" + crr_array[i] + "\t" + new_array[i] + "\t" + times[i]); 

} 

我所要的輸出是這樣的:

There are <count_of_repeated_element_sequences> repeated numbers 
<repeated_element>: <count> times 
... 

例如:

There are 3 repeated numbers: 
22: 2 times 
4: 3 times 
1: 2 times 

我如何找到重複的元素和它們的計數?我如何能如上所示打印它們?

+0

所以,** **什麼是問題呢?什麼是'y'?如何選擇更好的名稱,實際上告訴變量代表什麼?首先它會幫助你。 –

+0

你的輸入陣列在哪裏? – Makky

回答

4

這種問題可以通過字典(在Java中的HashMap)來輕鬆解決。

// The solution itself 
    HashMap<Integer, Integer> repetitions = new HashMap<Integer, Integer>(); 

    for (int i = 0; i < crr_array.length; ++i) { 
     int item = crr_array[i]; 

     if (repetitions.containsKey(item)) 
      repetitions.put(item, repetitions.get(item) + 1); 
     else 
      repetitions.put(item, 1); 
    } 

    // Now let's print the repetitions out 
    StringBuilder sb = new StringBuilder(); 

    int overAllCount = 0; 

    for (Map.Entry<Integer, Integer> e : repetitions.entrySet()) { 
     if (e.getValue() > 1) { 
      overAllCount += 1; 

      sb.append("\n"); 
      sb.append(e.getKey()); 
      sb.append(": "); 
      sb.append(e.getValue()); 
      sb.append(" times"); 
     } 
    } 

    if (overAllCount > 0) { 
     sb.insert(0, " repeated numbers:"); 
     sb.insert(0, overAllCount); 
     sb.insert(0, "There are "); 
    } 

    System.out.print(sb.toString()); 
+0

我希望只使用我想基礎,如果你可以再幫我沒有Java類 –

+0

感謝 我tryed它像 ////////的排序// 我該如何向您發送此評論不允許我粘貼代碼 –

+0

您是天才先生,該帖子確實幫助我完成了我的任務。非常感謝。 –

0
for (int i = 0; i < x.length; i++) { 

    for (int j = i + 1; j < x.length; j++) { 

     if (x[i] == x[j]) { 
      y[i] = x[i]; 
      times[i]++; 
     } 

    } 

} 
0

與O(N日誌(N))

int[] arr1; // your given array 
int[] arr2 = new int[arr1.length]; 
Arrays.sort(arr1); 

for (int i = 0; i < arr1.length; i++) { 
    arr2[i]++; 
    if (i+1 < arr1.length) 
    { 
     if (arr1[i] == arr1[i + 1]) { 
      arr2[i]++; 
      i++; 
     } 
    } 
} 

for (int i = 0; i < arr1.length; i++) { 
    if(arr2[i]>0) 
    System.out.println(arr1[i] + ":" + arr2[i]); 
} 
+0

這是O nlog(n)。不開)。 –

+0

hımm我想第二次,它似乎o(n),我不知道O(日誌(n)) –

+0

您正在排序數組,然後再進行處理。平均情況下的總體複雜度(取決於java排序的類型)是O(N * LogN)+ O(N),用於處理數組。 –

0

你必須使用或瞭解關聯數組,或地圖,..等。將重複元素的出現次數存儲在數組中,併爲重複元素本身保存另一個數組,沒有多大意義。

你在你的代碼的問題是在內部循環

for (int j = i + 1; j < x.length; j++) { 

     if (x[i] == x[j]) { 
      y[i] = x[i]; 
      times[i]++; 
     } 

    } 
5

如果你在很短的可能值的集合有值,則可以使用類似Counting Sort

如果不是必須使用另一種數據結構就像一個字典,在Java Map

int[] array 
Map<Integer, Integer> 

其中例如陣列[i]和值=計數器

示例鍵=數組值:

int[] array = new int [50]; 
Map<Integer,Integer> counterMap = new HashMap<>(); 

//fill the array 

    for(int i=0;i<array.length;i++){ 
     if(counterMap.containsKey(array[i])){ 
      counterMap.put(array[i], counterMap.get(array[i])+1); 
     }else{ 
      counterMap.put(array[i], 1); 
     } 
    } 
+0

這是一個偉大的代碼... –

+0

我只想使用基礎知識我希望如果你可以幫助我沒有Java類 –

2
public class DuplicationNoInArray { 

    /** 
    * @param args 
    *   the command line arguments 
    */ 
    public static void main(String[] args) throws Exception { 
     int[] arr = { 1, 2, 3, 4, 5, 1, 2, 8 }; 
     int[] result = new int[10]; 
     int counter = 0, count = 0; 
     for (int i = 0; i < arr.length; i++) { 
      boolean isDistinct = false; 
      for (int j = 0; j < i; j++) { 
       if (arr[i] == arr[j]) { 
        isDistinct = true; 
        break; 
       } 
      } 
      if (!isDistinct) { 
       result[counter++] = arr[i]; 
      } 
     } 
     for (int i = 0; i < counter; i++) { 
      count = 0; 
      for (int j = 0; j < arr.length; j++) { 
       if (result[i] == arr[j]) { 
        count++; 
       } 

      } 
      System.out.println(result[i] + " = " + count); 

     } 
    } 
} 
0
package jaa.stu.com.wordgame; 

/** 
* Created by AnandG on 3/14/2016. 
*/ 
public final class NumberMath { 
    public static boolean isContainDistinct(int[] arr) { 

     boolean isDistinct = true; 
     for (int i = 0; i < arr.length; i++) 

     { 

      for (int j = 0; j < arr.length; j++) { 
       if (arr[i] == arr[j] && i!=j) { 
        isDistinct = false; 
        break; 
       } 
      } 

     } 
     return isDistinct; 
    } 
    public static boolean isContainDistinct(float[] arr) { 

     boolean isDistinct = true; 
     for (int i = 0; i < arr.length; i++) 

     { 

      for (int j = 0; j < arr.length; j++) { 
       if (arr[i] == arr[j] && i!=j) { 
        isDistinct = false; 
        break; 
       } 
      } 

     } 
     return isDistinct; 
    } 
    public static boolean isContainDistinct(char[] arr) { 

     boolean isDistinct = true; 
     for (int i = 0; i < arr.length; i++) 

     { 

      for (int j = 0; j < arr.length; j++) { 
       if (arr[i] == arr[j] && i!=j) { 
        isDistinct = false; 
        break; 
       } 
      } 

     } 
     return isDistinct; 
    } 
    public static boolean isContainDistinct(String[] arr) { 

     boolean isDistinct = true; 
     for (int i = 0; i < arr.length; i++) 

     { 

      for (int j = 0; j < arr.length; j++) { 
       if (arr[i] == arr[j] && i!=j) { 
        isDistinct = false; 
        break; 
       } 
      } 

     } 
     return isDistinct; 
    } 
    public static int[] NumberofRepeat(int[] arr) { 

     int[] repCount= new int[arr.length]; 
     for (int i = 0; i < arr.length; i++) 

     { 

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

     } 
     return repCount; 
    } 
} 


call by NumberMath.isContainDistinct(array) for find is it contains repeat or not 

呼叫由INT []重複= NumberMath.NumberofRepeat(陣列),用於查找重複計數。每個單元包含多少相應的數組值重複...

-1
public class FindRepeatedNumbers 
{ 
public static void main(String[] args) 
    { 
    int num[]={1,3,2,4,1,2,4,6,7,5}; 
      Arrays.sort(num); 

    for(int j=1;j<num.length;j++) 
     { 
     if(num[j]==num[j-1]) 
    { 
      System.out.println(num[j]); 

     } 
    } 

     } 
    } 
0
public static void main(String[] args) { 
    Scanner input=new Scanner(System.in); 
    int[] numbers=new int[5]; 
    String x=null; 
    System.out.print("enter the number 10:"+"/n"); 
    for(int i=0;i<5;i++){ 
     numbers[i] = input.nextInt(); 
    } 
    System.out.print("Numbers : count"+"\n"); 
    int count=1; 
    Arrays.sort(numbers); 
    for(int z=0;z<5;z++){ 
     for(int j=0;j<z;j++){ 
      if(numbers[z]==numbers[j] & j!=z){ 
       count=count+1; 
      } 
     } 
     System.out.print(numbers[z]+" - "+count+"\n"); 
     count=1; 

    } 
0
public class ArrayDuplicate { 
private static Scanner sc; 
static int totalCount = 0; 

    public static void main(String[] args) { 
     int n, num; 
     sc = new Scanner(System.in); 
     System.out.print("Enter the size of array: "); 
     n =sc.nextInt(); 
     int[] a = new int[n]; 
     for(int i=0;i<n;i++){ 
      System.out.print("Enter the element at position "+i+": "); 
      num = sc.nextInt(); 
      a[enter image description here][1][i]=num; 
     } 
     System.out.print("Elements in array are: "); 
     for(int i=0;i<a.length;i++) 
      System.out.print(a[i]+" "); 
     System.out.println(); 
     duplicate(a); 
     System.out.println("There are "+totalCount+" repeated numbers:"); 
    } 

    public static void duplicate(int[] a){ 
     int j = 0,count, recount, temp; 
     for(int i=0; i<a.length;i++){ 
      count = 0; 
      recount = 0; 
      j=i+1; 
      while(j<a.length){ 
       if(a[i]==a[j]) 
        count++; 
       j++; 
      } 
      if(count>0){ 
       temp = a[i]; 
       for(int x=0;x<i;x++){ 
        if(a[x]==temp) 
         recount++; 
       } 
       if(recount==0){     
        totalCount++; 
        System.out.println(+a[i]+" : "+count+" times"); 
       } 
      } 

     } 
    } 

} 
0
package com.core_java; 

import java.util.Arrays; 
import java.util.Scanner; 

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

     Scanner input = new Scanner(System.in); 
     System.out.println("Enter array size: "); 
     int size = input.nextInt(); 

     int[] array = new int[size]; 

     // Read integers from the console 
     System.out.println("Enter array elements: "); 
     for (int i = 0; i < array.length; i++) { 
      array[i] = input.nextInt(); 
     } 
     Sim s = new Sim(); 
     s.find(array); 
    } 

    public void find(int[] arr) { 
     int count = 1; 
     Arrays.sort(arr); 

     for (int i = 0; i < arr.length; i++) { 

      for (int j = i + 1; j < arr.length; j++) { 
       if (arr[i] == arr[j]) { 
        count++; 
       } 
      } 
      if (count > 1) { 
       System.out.println(); 
       System.out.println("repeated element in array " + arr[i] + ": " + count + " time(s)"); 
       i = i + count - 1; 
      } 
      count = 1; 
     } 
    } 

} 
+0

最簡單的方法,我可以找到一個整數數組中重複元素的計數 – Manas