
- 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 duplicate element in a progression of first n terms JavaScript
Let’s say, we are given an array of numbers that contains first n natural numbers, but one element appears twice in the array, so the total number of elements is n+1. Our job is to write a function that takes in the array and returns the number that appears twice in linear time.
Method 1: Using Array.prototype.reduce()
This is a bit trickier approach but the most compressed in terms of code written. First, let’s see the code for it −
const arr = [1,4,8,5,6,7,9,2,3,7]; const duplicate = a => a.reduce((acc, val, ind) => val+acc- (ind+1))+a.length-1; console.log(duplicate(arr));
Here we have used the reduce function, its callback, which operates once for each element of array, in our case is taking three arguments,
- acc → accumulator, value returned in the previous pass, and
- val → the current element value,
- ind → the index of current element
Now, let’s apply our code to this array −
[ 2, 3, 1, 2]
As the length of this array is 4, there should be a total of 4 pass of callback function, but as we have not provided the initialValue argument to the reduce() function, iterations will start from index 1 and accumulator will be initially assigned with value at zeroth index, so there will be a total of 3 pass.
First Pass
acc = 2, val = 3, ind = 1 return value = 2+3 - (1+1) = 3
Second Pass
acc = 3, val = 1, ind = 2 return value = 3+1 - (2+1) = 1
Third Pass
acc = 1, val = 2, ind = 3 return value = 1+2 - (3+1) = -1
END OF ARRAY
Hence -1 gets returned from the array, and then
-1 + (4-1) = -1 + 3 = 2
gets returned from the duplicate() function, which is actually the correct result.
Method 2: Array.prototype.forEach()
In this method, we iterate over the array, get its sum, and subtract the sum of first (n-1) natural numbers from it, where n is the length of array, what gets left is the number that repeated twice, so we return it.
Example
const arr = [1,4,8,5,6,7,9,2,3,7]; const duplicate = a => { let sum = 0; const { length: n } = a; a.forEach(num => sum += num); return sum - ((n*(n-1))/2); } console.log(duplicate(arr));
Output
The output in the console will be −
7
- Related Articles
- The $m^{th}$ term of an arithmetic progression is $x$ and the $n^{th}$ term is $y$. Then find the sum of the first $( m+n)$ terms.
- Find first duplicate item in array in linear time JavaScript
- If the sum of first four terms of an A.P. is 40 and that of first 14 terms is 280. Find the sum of its first n terms.
- The sum of first 7 terms of an AP is 49 and that of 17 terms is 289. Find the sum of first n terms .
- In an arithmetic progression, if the $k^{th}$ term is $5k+1$, then find the sum of first $100$ terms.
- If the sum of first 7 terms of an AP is 49 and that of 17 terms is 289, find the sum of first $n$ terms.
- First element and last element in a JavaScript array?
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- If the sum of first $n$ terms of an A.P. is $n^2$, then find its 10th term.
- If the sum of the first $n$ terms of an A.P. is $4n – n^2$, what is the first term? What is the sum of first two terms? What is the second term? Similarly, find the third, the tenth and the $n$th terms.
- Write the first five terms of each of the following sequences whose nth terms are: \( a_{n}=3^{n} \)
- JavaScript code to find nth term of a series - Arithmetic Progression (AP)
- The sum of first \( n \) terms of an AP is given by \( \mathrm{S}_{n}=4 n^{2}+n \). Find that \( \mathrm{AP} \).
- Write the first five terms of each of the following sequences whose nth terms are:\( a_{n}=(-1)^{n} \cdot 2^{n} \)
- Write the first five terms of each of the following sequences whose nth terms are:\( a_{n}=\frac{n(n-2)}{2} \)
