
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 Last Occurrence of Element in Array in TypeScript
We will learn to find the last index of the element in the array in TypeScript. In development, an array of the data can contain duplicate data, and we may need to keep the last occurrence of the element.
For example, we have fetched all users' login history from the database. Now, we want to find when particular users were logged in for the last time. In such scenarios, we can use the below methods to find the last occurrence of the element in the array.
Search from the last in the array
To find the last occurrence of the element, we can search for the element from the last. As we find the first occurrence of the element from the last, we can keep that index as the last occurrence of the element from the start.
Syntax
Users can follow the syntax below to search for the last occurrence of the element in the array in TypeScript.
let simple_arr: Array<number> = [ 20, 232, 3, 21, 23, 343, 4, 4, 3, 3, 20, 20, 54, 43, ]; let element_to_search: number = 20; for (let i = simple_arr.length - 1; i >= 0; i--) { if (simple_arr[i] == element_to_search) { return i; // last index of element is i; } }
Algorithm
Step 1 ? Define the array of any data type.
Step 2 ? Use the for loop to iterate through the array from the last.
Step 3 ? Initialize the variable ?i' with the array's length-1, and iterate until ?i' becomes less than zero.
Step 4 ? Inside the for loop, check if we found a first match for the searching element and return the current index i.
Step 5 ? if we don't find the searching element after the complete iteration of the array, return -1.
Example
In the example below, we have created the function named searchFromLast(), which takes the searching element as a parameter and returns the last index of the searching element.
To find the last occurrence of the element in the array, we have implemented the above algorithm inside the searchFromLast() function.
// Creating the numbers array let simple_arr: Array<number> = [ 20, 232, 3, 21, 23, 343, 4, 4, 3, 3, 20, 20, 54, 43, ]; // function to find the last index of element function searchFromLast(element: number): number { // use the for loop to start searching from the last for (let i = simple_arr.length - 1; i >= 0; i--) { // return the first occurence of the element from the last if (simple_arr[i] == element) { return i; } } // if element not found, return -1 return -1; } // call the searchFromLast() function for various elements console.log( "The last occurence of the 20 in the array is at index " + searchFromLast(20) ); console.log( "The last occurence of the 3 in the array is at index " + searchFromLast(3) ); console.log( "The last occurence of the -3 in the array is at index " + searchFromLast(-3) );
On compiling, it will generate the following JavaScript code:
// Creating the numbers array var simple_arr = [ 20, 232, 3, 21, 23, 343, 4, 4, 3, 3, 20, 20, 54, 43, ]; // function to find the last index of element function searchFromLast(element) { // use the for loop to start searching from the last for (var i = simple_arr.length - 1; i >= 0; i--) { // return the first occurence of the element from the last if (simple_arr[i] == element) { return i; } } // if element not found, return -1 return -1; } // call the searchFromLast() function for various elements console.log("The last occurence of the 20 in the array is at index " + searchFromLast(20)); console.log("The last occurence of the 3 in the array is at index " + searchFromLast(3)); console.log("The last occurence of the -3 in the array is at index " + searchFromLast(-3));
Output
The above code will produce the following output ?
The last occurence of the 20 in the array is at index 11 The last occurence of the 3 in the array is at index 9 The last occurence of the -3 in the array is at index -1
Use the findLastIndex() method of TypeScript
In TypeScript, the findLastIndex() method is the built-in library method. We can use it to find the last occurrence of the particular element of the array. It takes the searching element as a parameter and returns its last index.
Syntax
Users can follow the syntax below to use the findLastIndex() method to search for the last occurrence of the element in TypeScript.
reference_arr.lastIndexOf(element);
Parameters
reference_arr ? It is an array in which we want to search for a particular element.
element ? It is a searching element for which we need to find the last occurrence.
Return Value
It returns the zero-based last index of the searching element if the element exists in the array; Otherwise, -1.
Example
In the below example, we have used the lastIndexOf() method with the array to find the last occurrence of various strings. We have created the array of strings containing the duplicate elements and then searched for the different elements in the example below.
// array containing the different values let string_arr: Array<string> = [ "Hi!", "Hello", "Hi!", "Hello", "Hi!", "Hello", "TutorialsPoint", "Hello", ]; // using the lastIndexOf() method with the array to search for particular elements console.log( "The last index of the hello in the array is " + string_arr.lastIndexOf("Hello") ); console.log( "The last index of the Hi! in the array is " + string_arr.lastIndexOf("Hi!") ); console.log( "The last index of the user in the array is " + string_arr.lastIndexOf("user") );
On compiling, it will generate the following JavaScript code:
// array containing the different values var string_arr = [ "Hi!", "Hello", "Hi!", "Hello", "Hi!", "Hello", "TutorialsPoint", "Hello", ]; // using the lastIndexOf() method with the array to search for particular elements console.log("The last index of the hello in the array is " + string_arr.lastIndexOf("Hello")); console.log("The last index of the Hi! in the array is " + string_arr.lastIndexOf("Hi!")); console.log("The last index of the user in the array is " + string_arr.lastIndexOf("user"));
Output
The above code will produce the following output ?
The last index of the hello in the array is 7 The last index of the Hi! in the array is 4 The last index of the user in the array is -1
We learned the custom algorithm to find the last occurrence of the element in the array. The custom algorithm is only for learning purposes, and users can use the lastIndexOf() method to achieve the same output.