5 kyu

Find number in an array # 1

79 of 130myjinxin2015

Description:

When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process --myjinxin2015 said

Description:

Given an unsorted array arr that contains some positive integers. It may be one of the following:

1. There are numbers 1 to n, only one number is 
   duplicate(repeated two times), the other numbers are unique. 
   That is, there are n+1 elements in the array. 
   e.g. [1,2,3,6,5,4,1]

2. There are numbers 1 to n, only one number is 
   unique, the other numbers are repeated two times. 
   That is, there are 2*n-1 elements in the array. 
   e.g. [1,2,3,1,2,3,4]

Your task is to determine the type of the array, if it is the first type, to return the duplicate; if it is second type, return the unique.

Note:

  • All numbers are positive integers that from 1 to n;
  • The length of array always more than 5;
  • Please pay attention to optimizing the code to avoid time out.

Some Examples

input                                output
[1,2,3,6,5,4,1]                      1
[1,2,3,1,2,3,4]                      4
[3,6,9,2,5,8,1,4,8,7]                8
[9,8,7,1,2,3,9,7,1,2,3,4,4,5,5,6,6]  8
Algorithms
Puzzles

Stats:

CreatedNov 30, 2016
PublishedDec 1, 2016
Warriors Trained629
Total Skips23
Total Code Submissions4074
Total Times Completed130
JavaScript Completions79
Python Completions53
Total Stars32
% of votes with a positive feedback rating90% of 46
Total "Very Satisfied" Votes39
Total "Somewhat Satisfied" Votes5
Total "Not Satisfied" Votes2
Total Rank Assessments7
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • Voile Avatar
  • towlerj Avatar
  • dfhwze Avatar
Ad