JavaScript Program to Find the First Non-Repeated Element in an Array
Finding the first non-repeated element in an array refers to identifying the initial occurrence of an element that does not occur again elsewhere within the array, indicating uniqueness among the elements.
Examples:
Input: {-1, 2, -1, 3, 0} Output: 2 Explanation: The first number that does not repeat is : 2 Input: {9, 4, 9, 6, 7, 4} Output: 6 Explanation: The first number that does not repeat is : 6
We have common approaches to perform this:
Table of Content
- Using For Loop in JavaScript
- Using find() Method
- Using map() Method
- Using Array Filter Method:
Approach 1 : Using For Loop in JavaScript
In this approach, we are using a nested for loop, iterate through each element in an array. Compare it with every other element to find the first non-repeated element. If found, print it; otherwise, indicate that all elements are repeated.
Syntax:
for (let i in obj1) {
// Prints all the keys in
// obj1 on the console
console.log(i);
}
Example: Below is the implementation of the above approach using nesting For loop.
let arr = [9, 4, 9, 6, 7, 4];
let n = arr.length;
let nonRepeatedFound = false;
for (let i = 0; i < n; i++) {
let j;
for (j = 0; j < n; j++) {
if (i != j && arr[i] == arr[j]) {
break;
}
}
if (j == n) {
console.log(
"The first non-repeated element is:",
arr[i]
);
nonRepeatedFound = true;
break;
}
}
if (!nonRepeatedFound) {
console.log(
"All elements in the array are repeated."
);
}
Output
The first non-repeated element is: 6
Approach 2: Using find() Method
In this approach, we use the find method, search for the first non-repeated element in an array. then we use a ternary operator to display either the element or a message indicating that all elements are repeated.
Syntax:
array.find(function(currentValue, index, arr),thisValue);
Example: Below is the implementation of the above approach using Find method.
let arr = [9, 4, 9, 6, 7, 4];
let nonRepeated = arr.find(
(num) =>
arr.indexOf(num) === arr.lastIndexOf(num)
);
let result =
nonRepeated !== undefined
? "The first non-repeated element is: " +
nonRepeated
: "All elements in the array are repeated.";
console.log(result);
Output
The first non-repeated element is: 6
Approach 3: Using map() Method
In this approach, map() method is used to iterate through an array, updating the count of each element using a Map, and then finding the first non-repeated element from the modified data.
Syntax:
map((element, index, array) => { /* … */ })
Example: In this example, The nonRepeatedElement function uses a Map to count element occurrences in an array using map(). It then finds and returns the first non-repeated element from the original array, or null if none.
function nonRepeatedElement(arr) {
/* Create a map to store
the count of each element */
const elementCount = new Map();
/* Use map to count the occurrences
of each element in the array */
arr.map((element) =>
elementCount.has(element)
? elementCount.set(
element,
elementCount.get(element) + 1
)
: elementCount.set(element, 1)
);
/* Iterate through the array again
to find the first non-repeated element */
for (let i = 0; i < arr.length; i++) {
if (elementCount.get(arr[i]) === 1) {
return arr[i];
}
}
/* If no non-repeated element is
found, return null or a default value */
return null;
}
const array = [9, 4, 9, 6, 7, 4];
const result = nonRepeatedElement(array);
console.log(result);
Output
6
Approach 4: Using Array Filter Method:
The approach filters elements by comparing their first and last occurrences in the array. Elements with different first and last indexes are returned, ensuring only non-repeated elements are included.
Syntax:
array.filter(callback(element, index, arr), thisValue)
Example: In this example we finds the first non-repeated element in an array. If found, it prints the element; otherwise, it indicates that all elements are repeated.
let arr = [9, 4, 9, 6, 7, 4];
let nonRepeated = arr.filter((num) =>
arr.indexOf(num) === arr.lastIndexOf(num)
)[0];
let result = nonRepeated !== undefined
? "The first non-repeated element is: " + nonRepeated
: "All elements in the array are repeated.";
console.log(result);
Output
The first non-repeated element is: 6