finding how many consecutive numbers in an array [java]

john

For example, array is like this {1,1,2,1,1,1 } and they key int is 1, the Largest number of consecutive times 1 is going to be 3. When i run my code and type the same numbers, I'm getting 4 consecutive numbers of 1. Can someone give me advice? is there a built-in method that will help me solve this?

 Scanner kbd = new Scanner (System.in);
            int count = 1;
            int largest = 0;

            System.out.println("Enter number");
            int numbers = kbd.nextInt();
            int[] numb = new int[numbers];
            System.out.println("Now enter "+numbers+" integers:");
            for(int i =0; i<numb.length;i++){
                numb[i] = kbd.nextInt();
            }
            System.out.println("Now enter the key integer: ");
            int key = kbd.nextInt();

            for (int i = 0; i<numb.length-1; i++) {
            if (numb[i] == key) {
                if (numb[i] == numb[i + 1]) {
                     count++;
                    }
            else {
                    largest = Math.max(largest, count);
                    count = 1;

                  }
           }
    }

            System.out.println("Largest number of consecutive times "+key+" was entered: "+largest);
Philip Tinney

A slightly different approach.

count = 0;
for (int i : numb){
  if (i == key){
    count++;
  } else {
    count = 0;
  }
  if(largest<count) {
    largest = count;
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding Consecutive Numbers in Java

From Dev

Find largest consecutive numbers in array and output numbers and how many there is

From Dev

Finding difference between consecutive numbers in an array in javascript

From Dev

finding how many times each element is in the array java

From Dev

finding how many times each element is in the array java

From Dev

Finding How Many Numbers are in a Double using Swift

From Dev

Java array - eliminate intermediary consecutive numbers

From Dev

generating random numbers in java and finding percentage of how many are less than or equal to 50

From Dev

generating random numbers in java and finding percentage of how many are less than or equal to 50

From Dev

How do I fill an array with consecutive numbers

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Finding the minimum value of int numbers in an array (Java)

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

How many ways to get a sum of consecutive whole numbers that total x?

From Dev

Finding the consecutive zeros in a numpy array

From Dev

Array into ranges of consecutive numbers

From Dev

Finding the largest group of consecutive numbers within a partition

From Dev

Finding the 4 consecutive numbers of the same value - horizontally

From Dev

Finding large changes in consecutive numbers in database

From Dev

Finding strings with consecutive characters in Java

From Dev

How to determine if an array has consecutive integers and if so, how many?

From Dev

How to determine if an array has consecutive integers and if so, how many?

From Dev

Finding specific numbers in an array?

From Dev

finding the sum of numbers in an array

From Dev

how many consecutive elements are smaller before each item in the array

From Dev

Finding prime numbers in Java

From Dev

Finding duplicate integers in an array and display how many times they occurred

From Dev

Java: Substitute consecutive numbers in a matrix

From Dev

Finding the primary numbers in an array of pointer

Related Related

  1. 1

    Finding Consecutive Numbers in Java

  2. 2

    Find largest consecutive numbers in array and output numbers and how many there is

  3. 3

    Finding difference between consecutive numbers in an array in javascript

  4. 4

    finding how many times each element is in the array java

  5. 5

    finding how many times each element is in the array java

  6. 6

    Finding How Many Numbers are in a Double using Swift

  7. 7

    Java array - eliminate intermediary consecutive numbers

  8. 8

    generating random numbers in java and finding percentage of how many are less than or equal to 50

  9. 9

    generating random numbers in java and finding percentage of how many are less than or equal to 50

  10. 10

    How do I fill an array with consecutive numbers

  11. 11

    Java - Finding Largest and Smallest Numbers using an Array

  12. 12

    Finding the minimum value of int numbers in an array (Java)

  13. 13

    Java - Finding Largest and Smallest Numbers using an Array

  14. 14

    How many ways to get a sum of consecutive whole numbers that total x?

  15. 15

    Finding the consecutive zeros in a numpy array

  16. 16

    Array into ranges of consecutive numbers

  17. 17

    Finding the largest group of consecutive numbers within a partition

  18. 18

    Finding the 4 consecutive numbers of the same value - horizontally

  19. 19

    Finding large changes in consecutive numbers in database

  20. 20

    Finding strings with consecutive characters in Java

  21. 21

    How to determine if an array has consecutive integers and if so, how many?

  22. 22

    How to determine if an array has consecutive integers and if so, how many?

  23. 23

    Finding specific numbers in an array?

  24. 24

    finding the sum of numbers in an array

  25. 25

    how many consecutive elements are smaller before each item in the array

  26. 26

    Finding prime numbers in Java

  27. 27

    Finding duplicate integers in an array and display how many times they occurred

  28. 28

    Java: Substitute consecutive numbers in a matrix

  29. 29

    Finding the primary numbers in an array of pointer

HotTag

Archive