JavaScript Program to Find Element that Appears once in Array where Other Elements Appears Twice
Last Updated :
04 Jun, 2024
Given an Array of Integers consisting of n elements where each element appears twice except one element. The task is to find the element which appears only once in the input array in JavaScript.
Example:
Input : arr = [ 5, 9, 2, 7, 4, 8, 6, 1, 5, 1, 4, 6, 3, 7, 8, 2, 9 ]
Output : 3
Explanation: The input array consists of 17 elements
where all elements except 3 appears twice i.e.
(1, 2, 4, 5, 6, 7, 8, 9) are repeating twice. Hence
the answer will be 3.
Input : arr = [ 4, 7, 1, 4, 1 ]
Output : 7
Approaches to Find the Element that Appears Once in Array where Every Other Element Appears Twice:
Using array filter() and indexOf() Methods
The first approach to solve the problem is to use the filter() method of arrays. It will return a new array say uniqueArr. Now perform the filter() method on the input array and check if the index of the current element in the array is equal to the last index of the element in the array. Because if they are equal then the element is unique. Print the first element of uniqueArr array.
Example: This example implements array filter() and indexOf() methods to get the element.
JavaScript
const arr = [
5, 9, 2, 7, 4, 6, 8, 1, 5,
1, 4, 6, 3, 7, 8, 2, 9
];
const uniqueArr = arr.filter((num) => {
return arr.indexOf(num) === arr.lastIndexOf(num);
});
console.log(uniqueArr);
Using forEach() Loop with XOR Operator
Another approach to solving the problem is to use the XOR operator (^).
- Initialize ans as 0.
- Now apply forEach loop in the array and for every element perform its XOR operation with ans and return ans.
- Since XOR is commutative and associative it will cancel out any duplicates in the array and only the unique element will be contained in ans.
- Print ans.
Example: This example uses Array filter and XOR operator to get required result
JavaScript
const arr = [
5, 9, 2, 7, 4, 6, 8, 1, 5,
1, 4, 6, 3, 7, 8, 2, 9
];
let ans = 0;
arr.forEach((num) => {
ans ^= num;
});
console.log(ans);
Using Map Data Structure
In this approach, we can utilize a JavaScript Map data structure to keep track of the count of each element in the array. Then, we iterate through the array, updating the count in the map. Finally, we iterate through the map and return the key whose value is 1, indicating that it appeared only once in the array.
Example:
JavaScript
function findSingleElement(arr) {
const countMap = new Map();
// Count the occurrences of each element in the array
arr.forEach(num => {
if (countMap.has(num)) {
countMap.set(num, countMap.get(num) + 1);
} else {
countMap.set(num, 1);
}
});
// Iterate through the map and find the element with count 1
for (const [key, value] of countMap.entries()) {
if (value === 1) {
return key;
}
}
// If no element with count 1 is found, return null or handle it as needed
return null;
}
const arr = [5, 9, 2, 7, 4, 6, 8, 1, 5, 1, 4, 6, 3, 7, 8, 2, 9];
console.log(findSingleElement(arr)); // Output: 3
Similar Reads
JavaScript Program to Print All Distinct Elements in an Integer Array Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array. Examples: Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ] Outpu
3 min read
Unique element in an array where all elements occur k times except one Given an array that contains all elements occurring k times, but one occurs only once. Find that unique element.Examples: Input : arr[] = {6, 2, 5, 2, 2, 6, 6} k = 3Output : 5Explanation: Every element appears 3 times accept 5. Input : arr[] = {2, 2, 2, 10, 2} k = 4Output: 10Explanation: Every eleme
13 min read
Find all elements in array which have at-least two greater elements Given an array of n distinct elements, the task is to find all elements in array which have at-least two greater elements than themselves. Examples : Input : arr[] = {2, 8, 7, 1, 5};Output : 2 1 5 Explanation:The output three elements have two or more greater elements Explanation:Input : arr[] = {7,
11 min read
Javascript Program for Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[]
4 min read
Remove elements from the array which appear more than k times Given an array of integers, remove all the occurrences of those elements which appear strictly more than k times in the array.Examples: Input : arr[] = {1, 2, 2, 3, 2, 3, 4} k = 2Output : 1 3 3 4Input : arr[] = {2, 5, 5, 7} k = 1Output : 2 7Approach: Take a hash map, which will store the frequency o
8 min read
Find elements which are present in first array and not in second Given two arrays, the task is that we find numbers which are present in first array, but not present in the second array. Examples : Input : a[] = {1, 2, 3, 4, 5, 10}; b[] = {2, 3, 1, 0, 5};Output : 4 10 4 and 10 are present in first array, butnot in second array.Input : a[] = {4, 3, 5, 9, 11}; b[]
14 min read
Array elements that appear more than once Given an integer array, print all repeating elements (Elements that appear more than once) in the array. The output should contain elements according to their first occurrences. Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}Output: 10 45 Input: arr[] = {1, 2, 3, 4, 2, 5}Output:2 Input: arr[
15+ min read
Count of elements in Array which are present K times & their double isn't present Given an array arr[] of N integers, the task is to find the count of elements in the array that are present K times and their double are not present in the array. Examples: Input: arr[] = {10, 6, 12, 8, 10, 8}, K = 2Output: 2Explanation: 10 is a valid number since it appears exactly two times and 2
9 min read
k-th distinct (or non-repeating) element among unique elements in an array. Given an integer array arr[], print kth distinct element in this array. The given array may contain duplicates and the output should print the k-th element among all unique elements. If k is more than the number of distinct elements, print -1.Examples:Input: arr[] = {1, 2, 1, 3, 4, 2}, k = 2Output:
7 min read
Find any one of the multiple repeating elements in read only array Given a read-only array of size ( n+1 ), find one of the multiple repeating elements in the array where the array contains integers only between 1 and n. A read-only array means that the contents of the array canât be modified.Examples: Input : n = 5 arr[] = {1, 1, 2, 3, 5, 4} Output : One of the nu
15 min read