Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting repeated elements in an integer array

Tags:

java

I have an integer array crr_array and I want to count elements, which occur repeatedly. First, I read the size of the array and initialize it with numbers read from the console. In the array new_array, I store the elements that are repeated. The array times stores the number of consecutive occurrences of an element. Then, I try to search for the repeating sequences and print them in a specific format. However, it does not work.

// 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]);

}

I want the output to look like this:

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

For example:

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

How can I find the repeated elements and their counts? How can I print them as shown above?

like image 688
Hany Moh. Avatar asked Jul 13 '13 13:07

Hany Moh.


People also ask

How do you count the number of repeating values in an array Java?

call by int[] repeat=NumberMath. NumberofRepeat(array) for find repeat count. Each location contains how many repeat corresponding value of array... Save this answer.

How do I count the number of repeated numbers in an array in C++?

Function findRepeat(int arr[],int n) takes an array and its length as input and displays the repeated element value and length of repeated elements. Take the initial count as 0. Starting from index i=0 to i<n.


2 Answers

This kind of problems can be easy solved by dictionaries (HashMap in Java).

  // 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());
like image 155
Dmitry Bychenko Avatar answered Oct 22 '22 07:10

Dmitry Bychenko


If you have values in a short set of possible values then you can use something like Counting Sort

If not you have to use another data structure like a Dictionary, in java a Map

int[] array
Map<Integer, Integer> 

where Key = array value for example array[i] and value = a counter

Example:

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);
         }
    }
like image 28
nachokk Avatar answered Oct 22 '22 07:10

nachokk