JavaScript Program for the Minimum Index of a Repeating Element in an Array
Last Updated :
16 Jul, 2024
Given an array of integers arr[], The task is to find the index of the first repeating element in it i.e. the element that occurs more than once and whose index of the first occurrence is the smallest. In this article, we will find the minimum index of a repeating element in an array in JavaScript.
Examples:
Input: arr[] = {10, 5, 3, 4, 3, 5, 6}
Output: 5
Explanation: 5 is the first element that repeats
Input: arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10}
Output: 6
Explanation: 6 is the first element that repeats
Approach 1: Brute force approach
Here we will use nested loops by using for to compare each element with every other element in the array to return duplicates.
Syntax:
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j]) {
minIndex = Math.min(minIndex, i);
}
}
}
Example: This example shows the use of the above-explained approach.
JavaScript
function findMinIndexBruteForce(arr) {
let minIndex = Infinity;
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1;
j < arr.length; j++) {
if (arr[i] === arr[j]) {
minIndex =
Math.min(minIndex, i);
}
}
}
return minIndex !== Infinity
? minIndex : -1;
}
const arr1 = [3, 2, 1, 2, 4, 3];
console.log(findMinIndexBruteForce(arr1));
Approach 2: Using an Object (Hash Map)
Here we will use an object to store the elements you have seen so far as keys and their corresponding indices as values. When we encounter a repeating element, check if its index is smaller than the current minimum index.
Syntax:
const seen = {};
JavaScript
function findMinIndexWithObject(arr) {
const seen = {};
let minIndex = Infinity;
for (let i = 0; i < arr.length; i++) {
if (seen[arr[i]] !== undefined) {
minIndex = Math
.min(minIndex, seen[arr[i]]);
} else {
seen[arr[i]] = i;
}
}
return minIndex !== Infinity
? minIndex : -1;
}
const arr2 = [3, 2, 1, 2, 4, 3];
console.log(findMinIndexWithObject(arr2));
Approach 3: Using a Set
Here we will use a set to keep track of the elements you have seen so far. When we encounter a repeating element, check if it exists in the set. If it does, compare its index with the current minimum index.
Syntax:
const seen = new Set();
JavaScript
function findMinIndexWithSet(arr) {
// Using a Map to store the indices of elements
const seen = new Map();
let minIndex = -1;
for (let i = 0; i < arr.length; i++) {
if (seen.has(arr[i])) {
// Check if we found a repeating element
const firstIndex = seen.get(arr[i]);
if (minIndex === -1 || firstIndex < minIndex) {
// If it's the first repeating element or
//it's closer to the beginning of the array
minIndex = firstIndex;
}
} else {
seen.set(arr[i], i);
}
}
return minIndex;
}
const arr3 = [3, 2, 1, 2, 4, 3];
console.log(findMinIndexWithSet(arr3));
Approach 4: Using an Array to Store Indices
In this approach, we utilize an array to store the indices of elements as we traverse the input array. When we encounter a repeating element, we can directly access its index from the array and compare it with the current minimum index.
- Create an array
indices
to store the indices of elements. - Initialize the
minIndex
variable to Infinity. - Iterate through the input array:
- If the current element already exists in the
indices
array, update minIndex
with the smaller value between the current minimum index and the index of the repeating element. - Otherwise, push the index of the current element into the
indices
array.
- After iterating through the entire array, check if
minIndex
has been updated. If it has, return minIndex
; otherwise, return -1, indicating that there are no repeating elements.
Example:
JavaScript
function findMinIndexWithArray(arr) {
const indices = []; // Array to store indices of elements
let minIndex = Infinity;
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
if (indices[element] !== undefined) {
minIndex = Math.min(minIndex, indices[element]);
} else {
indices[element] = i;
}
}
return minIndex !== Infinity ? minIndex : -1;
}
const arr = [3, 2, 1, 2, 4, 3];
console.log(findMinIndexWithArray(arr)); // Output: 0
Approach 5: Using Array Index and Value Swap Method
In this approach, we can rearrange the array elements by swapping each element with the value at its corresponding index. This way, we can detect the repeating element by checking if an element is already at its correct index.
Example:
JavaScript
function findFirstRepeatingIndex(arr) {
let n = arr.length;
for (let i = 0; i < n; i++) {
while (arr[i] !== i + 1) {
if (arr[i] === arr[arr[i] - 1]) {
return i; // Return the current index
}
let temp = arr[i];
arr[i] = arr[arr[i] - 1];
arr[temp - 1] = temp;
}
}
return -1;
}
let arr1 = [10, 5, 3, 4, 3, 5, 6];
console.log(findFirstRepeatingIndex(arr1));
let arr2 = [6, 10, 5, 4, 9, 120, 4, 6, 10];
console.log(findFirstRepeatingIndex(arr2));
Similar Reads
Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space) Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4
5 min read
Javascript Program for Least frequent element in an array Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them.Examples : Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1}Output : 33 appears minimum number of times in givenarray.Input : arr[] = {10, 20, 30}Output : 10 or 20 or
3 min read
Javascript Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9};
4 min read
Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value
3 min read
Minimum 0s to be inserted in Array such that no element is same as its index Given an array A = [A0, A1, A2, . . ., AN-1]. Perform the following operation: Total count of indices with value same as their positions.At each step, insert 0 at one of such positions.Repeat till no more elements exist whose value is same as the index. The task is to find the minimum number of inse
6 min read
Finding Minimum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic
3 min read
C Program to Find Minimum Value in Array In this article, we will learn how to find the minimum value in the array.The easiest and straightforward method is to iterate through each element of the array, comparing each value to an assumed minimum and updating it if the current value is less.C#include <stdio.h> int findMin(int arr[], i
3 min read
Javascript Program for Find the smallest missing number Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples:Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m =
4 min read
Find minimum value of arr[j] + abs(j - i) for all indices Given an array arr[] of size N, the task is for each index i (1â¤iâ¤n) find it's minimum value, that can be calculated by the formula (arr[j] + abs(jâi)), for 0<=j<n and j != i. Examples: Input: arr = { 1, 3, 11, 2, 15, 7, 5 }Output: 4 2 3 4 3 4 5Explanation: For i = 0, the minimum value is for
9 min read
Java Program to Remove Duplicate Elements From the Array Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted.Example:Java// Java Program to Remove Duplicate // Ele
6 min read