The Java code finds the first non-repeating element from an array using a queue and a frequency map (implemented as a HashMap).
import java.util.*; public class FirstNonRepeatingElement { public static int findFirstNonRepeating(int[] nums) { Queue<Integer> queue = new LinkedList<>(); Map<Integer, Integer> frequencyMap = new HashMap<>(); for (int num : nums) { frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); queue.offer(num); } while (!queue.isEmpty()) { int num = queue.poll(); if (frequencyMap.get(num) == 1) { return num; } } return -1; // If no non-repeating element found } public static void main(String[] args) { int[] nums = {10, 20, 20, 30, 40, 40, 50}; int result = findFirstNonRepeating(nums); System.out.println("First Non-repeating Element : " + result); } }
First Non-repeating Element : 10
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions