CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Daniel Buyinza
Level 13
Kampala
  • 12.06.2019
  • 814views
  • 1comment

Failed to pass the last requirement

Question about the task Longest sequence
Java Syntax,  Level 8,  Lesson 6
Under discussion

1. Create a list of numbers.
2. Use the keyboard to add 10 numbers to the list.
3. Display the length of the longest sequence of repeating numbers in the list.

Example for the list 2, 4, 4, 4, 8, 8, 4, 12, 12, 14:
3

The value is 3, because the longest sequence of repeating numbers is three fours.

Requirements:
  • The program must display a number on the screen.
  • The program should read values from the keyboard.
  • In the main method, declare an ArrayList variable with Integer elements and immediately initialize it.
  • The program should add 10 numbers to the collection in accordance with the conditions.
  • The program should display the length of the longest sequence of repeating numbers in the list.
package com.codegym.task.task08.task0812; import java.io.*; import java.util.ArrayList; import java.util.Collections; /* Longest sequence */ public class Solution { public static void main(String[] args) throws IOException { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<Integer> numbers = new ArrayList<>(); ArrayList<Integer> repetitions = new ArrayList<>(); // Adding numbers to the list for (int n = 0; n < 10; n++) numbers.add(Integer.parseInt(reader.readLine())); // Checking for repeated numbers Integer sequence = 1; for (int n = 0, nxt = 1; n < 10 && nxt < 10; n++, nxt++) { while (!numbers.get(n).equals(numbers.get(nxt))) { n++; nxt++; sequence = 1; if (n == 8 && nxt == 9) { repetitions.add(sequence); break; } } while (numbers.get(n).equals(numbers.get(nxt))) { sequence++; if (n == 8 && nxt == 9) { repetitions.add(sequence); break; } n++; nxt++; if (!numbers.get(n).equals(numbers.get(nxt))) { repetitions.add(sequence); sequence = 1; break; } } } Collections.sort(repetitions); System.out.println(repetitions.get(repetitions.size() - 1)); } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
John Michael Montuya
Level 10 , Cebu City, Philippines
14 June 2019, 15:48
I'm thinking that we have different algorithms since your code looks rather complicated to me, but the way I did it was to declare two variables - count and finalCount wherein finalCount keeps a record of the longer number sequence by far until the array is fully traversed.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.