Java: Find the length of the longest consecutive elements sequence from a given unsorted array of integers
Java Array: Exercise-34 with Solution
Write a Java program to find the length of the longest consecutive elements sequence from an unsorted array of integers.
Sample array: [49, 1, 3, 200, 2, 4, 70, 5]
The longest consecutive elements sequence is [1, 2, 3, 4, 5], therefore the program will return its length 5.
Pictorial Presentation:
Sample Solution:
Java Code:
// Import the HashSet class from the java.util package.
import java.util.HashSet;
// Define a class named Exercise34.
public class Exercise34 {
// The main method for executing the program.
public static void main(String[] args) {
// Declare an array of integers.
int nums[] = {49, 1, 3, 200, 2, 4, 70, 5};
// Print the original array length.
System.out.println("Original array length: " + nums.length);
// Print the elements of the array.
System.out.print("Array elements are: ");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
// Calculate the new length of the array using the longest_sequence method and print it.
System.out.println("\nThe new length of the array is: " + longest_sequence(nums));
}
// Define a method named longest_sequence that takes an array of integers as input.
public static int longest_sequence(int[] nums) {
// Create a HashSet to store unique values from the input array.
final HashSet h_set = new HashSet();
for (int i : nums) {
h_set.add(i);
}
// Initialize a variable to store the length of the longest sequence.
int longest_sequence_len = 0;
// Iterate through the elements of the input array.
for (int i : nums) {
int length = 1;
for (int j = i - 1; h_set.contains(j); --j) {
h_set.remove(j);
++length;
}
for (int j = i + 1; h_set.contains(j); ++j) {
h_set.remove(j);
++length;
}
// Update the longest sequence length if a longer sequence is found.
longest_sequence_len = Math.max(longest_sequence_len, length);
}
// Return the length of the longest sequence.
return longest_sequence_len;
}
}
Sample Output:
Original array length: 8 Array elements are: 49 1 3 200 2 4 70 5 The new length of the array is: 5
Flowchart:
Java Code Editor:
Previous: Write a Java program to remove the duplicate elements of a given array and return the new length of the array.
Next: Write a Java program to find the sum of the two elements of a given array which is equal to a given integer.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/array/java-array-exercise-34.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics