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
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);
}
}
You have to use or read about associative arrays, or maps,..etc. Storing the the number of occurrences of the repeated elements in array, and holding another array for the repeated elements themselves, don't make much sense.
Your problem in your code is in the inner loop
for (int j = i + 1; j < x.length; j++) {
if (x[i] == x[j]) {
y[i] = x[i];
times[i]++;
}
}
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");
}
}
}
}
}
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;
}
}
}
private static void getRepeatedNumbers() {
int [] numArray = {2,5,3,8,1,2,8,3,3,1,5,7,8,12,134};
Set<Integer> nums = new HashSet<Integer>();
for (int i =0; i<numArray.length; i++) {
if(nums.contains(numArray[i]))
continue;
int count =1;
for (int j = i+1; j < numArray.length; j++) {
if(numArray[i] == numArray[j]) {
count++;
}
}
System.out.println("The "+numArray[i]+ " is repeated "+count+" times.");
nums.add(numArray[i]);
}
}
with O(n log(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]);
}