2013-07-13 159 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