- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- 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

# Find Smallest Letter Greater Than Target in JavaScript

Suppose we are given an array of sorted characters letters containing only lowercase letters. And given a target letter target.

We are required to write a JavaScript function that takes in the array as the first argument and the letter as the second. The function is supposed to find the smallest element in the list that is larger than the given target.

We have to keep in mind that letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'.

For Example −

If the input array and letter are −

const arr = ["c", "f", "j"]; const target = "a";

Then the output should be −

const output: "c";

## Example

The code for this will be −

const arr = ["c", "f", "j"]; const target = "a"; const findNearestLetter = (arr = [], target = '') => { let left = 0; let right = arr.length - 1; while (left <= right) { let mid = left + (right - left) / 2; if (arr[mid] <= target) { left ++; } else { right --; }; }; if (left == arr.length) { return arr[0]; }; return arr[left]; }; console.log(findNearestLetter(arr, target));

## Output

And the output in the console will be −

c

- Related Questions & Answers
- Find Smallest Letter Greater Than Target in Python
- Find smallest element greater than K in Python
- How to find the smallest number greater than x in Python?
- Smallest prime number just greater than the specified number in JavaScript
- Program to find lowest sum of pairs greater than given target in Python
- JavaScript - Find the smallest n digit number or greater
- How to get the smallest integer greater than or equal to a number in JavaScript?
- Python - Find words greater than given length
- Product of subarray just less than target in JavaScript
- Find Strings greater than a particular length in MongoDB?
- Finding element greater than its adjacent elements in JavaScript
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- Program to find size of smallest sublist whose sum at least target in Python
- Find the Next perfect square greater than a given number in C++
- Queries for greater than and not less than using C++

Advertisements