Java: check if there are three or more matches in an array

I want to make a function that checks if any number exists in an array that exists three or more times.

So, an example of this array:

4 - 6 - 14 - 8 - 6 - 15 - 14 - 15 - 13 - 10 -

Should not output anything, but this:

4 - 6 - 14 - 8 - 6 - 15 - 14 - 15 - 14 - 10 -

If the system prints, the number 14 exists three times.

How to do it? I started creating a for loop,

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

And then I got stuck, how can I do this?

+3
source share
7 answers
for (int i = 0; i < array.length; i++) {
    int count = 0;
    for (int j = 0; j < array.length; j++) {
        {
            if (array[i] == array[j]) {
                count++;
            }

        }
        if (count >= 3) {
            System.out.println(array[i] + " exists " + count
                    + " times.");
        }
    }
}

n ( n >= 3), n . , , . , , . : HashMap List... ....

, Integer[] int[] :

Integer[] array = new Integer[] { 1, 2, 1, 1, 1, 3, 3, 3, 2, 2, 4, 5 };

for (int i = 0; i < array.length; i++) {
    int count = 0;
    if (array[i] != null) {
        int compare = array[i].intValue();
        for (int j = 0; j < array.length; j++) {

            if (array[j] != null) {
                if (compare == array[j]) {
                    array[j] = null;
                    count++;
                }
            }

        }
        if (count >= 3) {
            System.out
                    .println(compare + " exists " + count + " times.");
        }
    }
}

:

1 exists 4 times.
2 exists 3 times.
3 exists 3 times.

, Integer []. .

+3

hashmap . for , 3

+3

, . - . reset

+1

, + 1, , , 0, , 2.

private static int findTree(int[] array){


        Arrays.sort(array);

        int i = 0;
        int counter = 0;

        while (counter != 2 && i < array.length - 1) {

            if(array[i] == array[i+1]){
                counter++;
            } else {
                counter =0;
            }

            i++;
        }

        if(counter == 2){
            return array[i];
        }

        return -1;
    }

:

alg, n .

, , n .

, , , (n) , (i) , (i + -1). , , , (i ++).

:

private static int findN(int[] array, int n){

        if(n == 0) {
         return -1;
        }

        Arrays.sort(array);

        n--; //We reduce our n searched element because in i we already have one

        int i = 0;
        boolean found = false;

        while (found == false && i < array.length - n) {
            if(array[i] == array[i+n]) { //remember that we have reduced the n by one.
                found = true;
            } else {
                i++;
            }
        }

        if(found) {
           return array[i];
        }

        return -1;
    }
+1

, 4 ?

  • , 3 4, 3
  • 1x , 3
  • 2x, 3
0

, 3 ?

  • ,
0

: , X.

public class TripleTest
{
    public static void main (String[] args)
    {
        int[] list = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
        findCount (list, 3);
    }

    public static void findCount (int[] list, int count)
    {
        for (int i = 0; i <= list.length - count; ++i)
        {
            int elem = list [i];
            boolean hit = findWhatCountFrom (list, i + 1, elem, count - 1);
            if (hit) 
                System.out.println ("hit: " + elem);
        }
    }

    public static boolean findWhatCountFrom (int[] list, int idx, int what, int count)
    {
        if (count == 0)
            return true;
        if (idx == list.length) 
            return false;
        return findWhatCountFrom (list, idx + 1, what, count - ((list [idx] == what) ? 1 : 0));
    }
}

, , , . . - .

, int [] Generics. , Array List.

0

All Articles