 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Take an array and find the one element that appears an odd number of times in JavaScript
Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times. There will always be only one integer that appears an odd number of times.
We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.
Example
Following is the code −
const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => {    let count = 0;    let last;    arr.sort((a, b) => a - b);    for (let i = 0; i < arr.length; i++){       if (arr[i] === last) {          count++;          continue;       };       if(count % 2){          return last;       };       last = arr[i];       count = 1;    };    return last; }; console.log(findOdd(arr));  Output
This will produce the following output −
5
Advertisements
 