Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Problem Statement
Given:
An array of integers.
Task:
Find the first element that repeats. The second occurrence should be the
earliest among all repeating elements.
Return:
The repeating element. If none exists, return -1.
🧾 Sample Input/Output
Input:
int[] arr = {10, 5, 3, 4, 3, 5, 6};
Output:
5
Explanation:
Both 5 and 3 repeat, but 5 repeats first in the array.
🔍 Naive Brute-Force
Approach
Compare every element with every other element that comes
after it.
public
static int firstRepeatingNaive(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length;
j++) {
if (arr[i] == arr[j]) {
return arr[i];
}
}
}
return -1;
}
⛔ Time Complexity: O(n²)
⛔
Space Complexity: O(1)
✅ Works, but too slow for large
arrays.
⚡ Optimized Approach Using
HashSet
Track seen elements using a Set while traversing the array in
reverse.
import
java.util.*;
public
class RepeatingElement {
public static int
firstRepeatingElement(int[] arr) {
Set<Integer> seen = new
HashSet<>();
int result = -1;
for (int i = arr.length - 1; i >= 0;
i--) {
if (seen.contains(arr[i])) {
result = arr[i]; // This is the
earliest repeated from right
} else {
seen.add(arr[i]);
}
}
return result;
}
public static void main(String[] args) {
int[] arr = {10, 5, 3, 4, 3, 5, 6};
System.out.println(firstRepeatingElement(arr)); // Output: 5
}
}
✅ Why Reverse Loop?
Because it ensures we find the first repeating element
(in original order) — the earliest second occurrence.
🧠 Edge Cases
Input |
Output |
Explanation |
{1, 2, 3, 4, 5} |
-1 |
No repeats |
{2, 2, 3, 4, 5} |
2 |
Immediate repeat at the start |
{5, 1, 2, 3, 1, 2} |
1 |
1 repeats before 2 again |
{} |
-1 |
Empty array |
📈 Time & Space
Complexity
Complexity |
Value |
Time |
O(n) |
Space |
O(n) (for HashSet) |
🧨 Interview Variants
✅ Recap: Key Takeaways
Because
they test core programming logic, data handling, loops, and algorithm
efficiency.
int[] is a primitive array; Integer[] is an array of objects (wrappers). The latter allows null values and works with collections.
For fixed-size problems, use arrays. For dynamic data, ArrayList is better — but stick to arrays unless otherwise asked
Index out of bounds, mutating arrays while iterating, and forgetting that Java arrays have fixed size.
Use a Set for uniqueness or sort the array first and remove duplicates in-place using two pointers.
It’s great for sorted arrays, especially for problems involving pair sums, removing duplicates, and reversing data in-place.
Use reversal techniques or cyclic replacements to do it in O(1) space.
Linear search = O(n); binary search = O(log n), but only on sorted arrays.
Always consider edge cases: empty arrays, one element, all duplicates, etc. Optimize with hashmaps or prefix sums where needed.
Use Arrays.sort(), System.arraycopy(), Arrays.toString(), and Collections when applicable — but show the manual solution first in interviews.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)