Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Removing the odd occurrence of any number/element from an array in JavaScript
When working with arrays, you might need to remove elements that appear an odd number of times (excluding elements that appear only once). This tutorial shows how to implement this functionality in JavaScript.
Problem Statement
Given an array of numbers, we need to remove the last occurrence of any element that appears an odd number of times, but keep elements that appear only once.
const arr = [1, 6, 3, 1, 3, 1, 6, 3];
console.log("Original array:", arr);
Original array: [1, 6, 3, 1, 3, 1, 6, 3]
In this example, number 1 appears 3 times (odd), number 3 appears 3 times (odd), and number 6 appears 2 times (even). We should remove the last occurrence of 1 and 3.
Expected Output
[1, 6, 3, 1, 3, 6]
Algorithm Approach
We'll use a hashmap to track occurrences and indices, then remove the last occurrence of elements with odd frequency (excluding single occurrences).
Implementation
const arr = [1, 6, 3, 1, 3, 1, 6, 3];
const removeOddOccurrence = (arr = []) => {
// Keep the original array unaltered
const copy = arr.slice();
const map = {};
// Track frequency and last index for each element
arr.forEach((num, ind) => {
if (map.hasOwnProperty(num)) {
map[num][0]++;
map[num][1] = ind;
} else {
map[num] = [1, ind];
}
});
// Remove last occurrence of elements with odd frequency > 1
for (const key in map) {
const [freq, index] = map[key];
if (freq !== 1 && freq % 2 === 1) {
copy.splice(index, 1, '');
}
}
// Filter out empty strings
return copy.filter(el => el !== '');
};
console.log("Result:", removeOddOccurrence(arr));
Result: [1, 6, 3, 1, 3, 6]
How It Works
The algorithm works in three steps:
- Frequency Mapping: Create a map where each key stores [frequency, lastIndex]
- Identify Targets: Find elements with odd frequency greater than 1
- Remove Elements: Replace the last occurrence with empty string, then filter
Alternative Approach Using Map
const removeOddOccurrenceMap = (arr) => {
const frequencyMap = new Map();
const result = [];
// Count frequencies
arr.forEach(num => {
frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1);
});
// Build result array, skipping last occurrence of odd-frequency elements
for (let i = 0; i < arr.length; i++) {
const num = arr[i];
const freq = frequencyMap.get(num);
if (freq === 1 || freq % 2 === 0) {
result.push(num);
} else {
// For odd frequency > 1, skip the last occurrence
const lastIndex = arr.lastIndexOf(num);
if (i !== lastIndex) {
result.push(num);
}
}
}
return result;
};
const testArray = [1, 6, 3, 1, 3, 1, 6, 3];
console.log("Alternative result:", removeOddOccurrenceMap(testArray));
Alternative result: [1, 6, 3, 1, 3, 6]
Conclusion
Both approaches successfully remove the last occurrence of elements appearing an odd number of times. The first method uses array manipulation with splice, while the second builds a new array directly, which can be more efficient for larger datasets.
