
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Find the element that appears once in sorted array - JavaScript
Suppose, we have a sorted array of literals like this −
const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9];
We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array. If there is no such number in the array, we should return false.
For this array, the output should be 6
Example
Following is the code −
const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; const firstNonDuplicate = arr => { let appeared = false; for(let i = 0; i < arr.length; i++){ if(appeared){ if(arr[i+1] !== arr[i]){ appeared = false; }; }else{ if(arr[i+1] === arr[i]){ appeared = true; continue; }; return arr[i]; }; }; return false; }; console.log(firstNonDuplicate(arr));
Output
Following is the output in the console −
6
- Related Articles
- Find the element that appears once in an array where every other element appears twice in C++
- 8085 program to find the element that appears once
- Take an array and find the one element that appears an odd number of times in JavaScript
- Return the element that appears for second most number of times in the array JavaScript
- Finding two missing numbers that appears only once and twice respectively in JavaScript
- Find the only element that appears b times using C++
- Finding first unique element in sorted array in JavaScript
- Get the item that appears the most times in an array JavaScript
- Finding the first unique element in a sorted array in JavaScript
- Checking for majority element in a sorted array in JavaScript
- Nth smallest element in sorted 2-D array in JavaScript
- First element that appears even number of times in an array in C++
- How to find the one integer that appears an odd number of times in a JavaScript array?
- How to find every element that exists in any of two given arrays once using JavaScript?
- Finding smallest element in a sorted array which is rotated in JavaScript

Advertisements