class Solution {
public int firstNonRepeating(int[] arr) {
Map<Integer, Integer> map = new LinkedHashMap<>();
for (int num : arr) map.put(num, map.getOrDefault(num, 0) + 1);
for (int num : map.keySet()) if (map.get(num) == 1) return num;
return -1;
}
}
Explanation: LinkedHashMap preserves insertion order to find the first non-repeating element efficiently.
β± O(n) | πΎ O(n)