user1859040
user1859040

Reputation: 83

How to count how many integer elements in array that are equal in java?

Say I have an array of 3 integers. I want to be able to obtain these results from these arrays using just if statements or a for loop.

[0,1,2] = 0 equal
[0,1,0] = 2 equal
[0,0,0] = 3 equal

This what i have so far and it works, but I think it could be simplified.

int numequal = 0;

if(intarr[0] != null && intarr[1] != null && intarr[0].numequals(intarr[1])) {
    numequal++;
}

if(intarr[0] != null && intarr[2] != null && intarr[0].numequals(intarr[2])) {
    numequal++;
}

if(intarr[1] != null && intarr[2] != null && intarr[1].numequals(intarr[2])) {
    numequal++;
}

if(numequal == 1) {
    numequal = 2;
}

Also, I'm trying to keep it basic. Maybe just for loops. No hash sets or dictionaries.

Upvotes: 3

Views: 5138

Answers (5)

d.raev
d.raev

Reputation: 9546

You are probably looking for really simple solution so I tried to optimize your code a bit:

int[] intarr = {'0','1','2'};
int numequal = 0; 

if(intarr[0] == intarr[1] || intarr[0] == intarr[2] ){
    numequal++;
}
if( intarr[1] == intarr[2]){
    numequal++;
}

if (numequal > 0 ){
    numequal++;
}

if your array is declared as int[] there is no need to check for nulls
intarr[1] != null.

If some item is not set, it will be default 0

Upvotes: 1

Alejandro Guinea
Alejandro Guinea

Reputation: 11

I believe this is the easiest way (some may beg to differ):

int[] x = { 1, 1, 2, 4, 6, 7, 5, 6, 4, 2, 3, 1, 6, 6, 5, 6, 5, 6, 4, 5, 9,
    7, 8, 6, 5, 4, 6 };
int rep = 0;
int finalc = 0;

for (int i = 0; i < x.length; i++) {
  int basec = 0;
  for (int j = 0; j < x.length; j++) {
    if (x[i] == x[j]) {
      basec++;
    }
  }
  if (basec > finalc) {
    finalc = basec;
    rep = x[i];
  }
}

System.out.println("The number " + rep + " is repeated " + finalc +" times");

This will print:

The number 6 is repeated 8 times

Upvotes: 1

d.raev
d.raev

Reputation: 9546

you can use Array.sort and count them nicely:

    int[] a = {0,1, 2, 1, 3, 0, 1 };
    int size = 0;
    int counter = 0;
    //sort it and you will get all the equal inters in a sequance.
    Arrays.sort(a);// {0,0,1,1,2,3}
    for(int i = 1; i < a.length; i++){          
        if ( a[i-1] == a[i]){
            counter++;
            if(counter  > size){
                size = counter;
            }
        }else{
            counter = 0;
        }
    }
    return size;

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

Here is my solution. It is not simple but very efficient. Currently it counts null elements too, which can easily be fixed if it's undesirable.

    Integer[] a = { null, 2, 1, null, 0, 1 };
    Arrays.sort(a, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            if (o1 == null) {
                return -1;
            }
            if (o2 == null) {
                return 1;
            }
            return o1.compareTo(o2);
        }
    });
    // [null, null, 0, 1, 1, 2]
    int n = 0;
    Integer prev = null;
    Boolean first = null;
    for (Integer e : a) {
        if (first != null && (e == prev || e != null && e.equals(prev))) {
            if (first == Boolean.TRUE) {
                n += 2;
                first = Boolean.FALSE;
            } else {
                n++;
            }
        } else {
            first = Boolean.TRUE;
        }
        prev = e;
    }
    System.out.println(n);

Upvotes: 1

user529543
user529543

Reputation:

This is an algorithm question, which can be optimizez.....

I would use an un-synchronized key-value storage, for the key I would put the int as string, for the value his counts.

Frtst you will try to get it like: hashMap.get("0") if it return null, than the count is 0, than put it back: hashMap.put("0", new Integer(1)).

Unless you don't need, than use the unsynchronezed version of hashMap, searc it with google!

Upvotes: 0

Related Questions