Java Online Compiler
Example: NonRepeatingElementsFrequencyArray in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// NonRepeatingElementsFrequencyArray public class Main { public static void main(String[] args) { int[] arr = {5, 8, 2, 5, 8, 1, 9}; // Assuming elements are within 0-10 (adjust MAX_VALUE as needed) final int MAX_VALUE = 10; System.out.println("Non-repeating elements (Frequency Array):"); // Step 1: Create a frequency array, initialized to zeros // The size should be MAX_VALUE + 1 to accommodate MAX_VALUE itself int[] frequencyArray = new int[MAX_VALUE + 1]; // Step 2: Iterate through the input array and populate the frequency array for (int element : arr) { // Ensure element is within bounds if (element >= 0 && element <= MAX_VALUE) { frequencyArray[element]++; } else { System.out.println("Warning: Element " + element + " out of assumed range [0, " + MAX_VALUE + "]"); } } // Step 3: Iterate through the original array to find elements with frequency 1 // (maintaining original order of first appearance) for (int element : arr) { if (element >= 0 && element <= MAX_VALUE && frequencyArray[element] == 1) { System.out.println(element); // Optional: Mark as processed if you only want to print once even if it appears multiple times as unique in the first loop // frequencyArray[element] = 0; } } } }
Output
Clear
ADVERTISEMENTS