Find the first repeating element in an array of integers.
👋 Hey there! I’m Shohanur Rahman!
I’m a backend developer with over 5.5 years of experience in building scalable and efficient web applications. My work focuses on Java, Spring Boot, and microservices architecture, where I love designing robust API solutions and creating secure middleware for complex integrations.
💼 What I Do Backend Development: Expert in Spring Boot, Spring Cloud, and Spring WebFlux, I create high-performance microservices that drive seamless user experiences. Cloud & DevOps: AWS enthusiast, skilled in using EC2, S3, RDS, and Docker to design scalable and reliable cloud infrastructures. Digital Security: Passionate about securing applications with OAuth2, Keycloak, and digital signatures for data integrity and privacy. 🚀 Current Projects I’m currently working on API integrations with Spring Cloud Gateway and designing an e-invoicing middleware. My projects often involve asynchronous processing, digital signature implementations, and ensuring high standards of security.
📝 Why I Write I enjoy sharing what I’ve learned through blog posts, covering everything from backend design to API security and cloud best practices. Check out my posts if you’re into backend dev, cloud tech, or digital security!
To find the first repeating element in an array of integers, we can iterate through the array and use a HashMap to keep track of the frequency of each element. The first element that has a frequency greater than 1 is the first repeating element.
Here's the Java code:
import java.util.HashMap;
public class FirstRepeatingElement {
public static Integer findFirstRepeatingElement(int[] arr) {
// create a HashMap to keep track of the frequency of each element
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
// iterate through the array and update the frequency in the HashMap
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(arr[i])) {
// if the element is already in the HashMap, increment its frequency
map.put(arr[i], map.get(arr[i]) + 1);
} else {
// otherwise, add the element to the HashMap with a frequency of 1
map.put(arr[i], 1);
}
}
// iterate through the array again and return the first element that has a frequency greater than 1
for (int i = 0; i < arr.length; i++) {
if (map.get(arr[i]) > 1) {
return arr[i];
}
}
// if no repeating element is found, return null
return null;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5};
Integer firstRepeatingElement = findFirstRepeatingElement(arr);
System.out.println("The first repeating element is: " + firstRepeatingElement);
}
}
In this example, the input array is {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5}, and the expected output is 5.
The time complexity of this program is O(n), where n is the length of the input array.
This is because we iterate through the array twice, each iteration taking O(n) time. The first iteration updates the frequency of each element in the HashMap, and the second iteration finds the first element with a frequency greater than 1.
In the worst case, where there are no repeating elements, we will still need to iterate through the entire array once and perform constant-time operations on each element. Therefore, the time complexity is O(n).
The space complexity is also O(n), as we need to store the frequency of each element in the HashMap. The worst-case scenario is when all the elements are distinct, and we need to store n key-value pairs in the HashMap.