Find a duplicate element in a limited range array

Given a limited range array of size n where array contains elements between 1 to n - 1 with one element repeating, find the duplicate number in this array.

Example Solution

Let array arr equal to [ 1, 2, 3, 4, 4 ]. The duplicate element in this array is 4, therefore the solution should return the integer 4.

Let array arr equal to [ 1, 2, 3, 2, 4 ]. The duplicate element in this array is 2, therefore the solution should return the integer 2.

Contributed by Murat Sütunç