Java Online Compiler
Example: NonRepeatingElementsHashMap in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// NonRepeatingElementsHashMap import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { int[] arr = {5, 8, 2, 5, 8, 1, 9}; System.out.println("Non-repeating elements (HashMap):"); // Step 1: Create a HashMap to store element frequencies Map<Integer, Integer> frequencyMap = new HashMap<>(); // Step 2: Iterate through the array and populate the frequency map for (int element : arr) { frequencyMap.put(element, frequencyMap.getOrDefault(element, 0) + 1); } // Step 3: Iterate through the array again to maintain original order // and print elements with a frequency of 1 for (int element : arr) { if (frequencyMap.get(element) == 1) { System.out.println(element); } } // Alternative Step 3: Iterate through the map (order not guaranteed) // for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) { // if (entry.getValue() == 1) { // System.out.println(entry.getKey()); // } // } } }
Output
Clear
ADVERTISEMENTS