# Task 2: Duplicate and Missing # Submitted by: Mohammad S Anwar # # You are given an array of integers in sequence with one missing and one duplicate. # Write a script to find the duplicate and missing integer in the given array. Return -1 if none found. # For the sake of this task, let us assume the array contains no more than one duplicate and missing. # # Example 1: # Input: @nums = (1,2,2,4) # Output: (2,3) # Duplicate is 2 and Missing is 3. # # Example 2: # Input: @nums = (1,2,3,4) # Output: -1 # No duplicate and missing found. # # Example 3: # Input: @nums = (1,2,3,3) # Output: (3,4) # Duplicate is 3 and Missing is 4. def duplicate_and_missing(list1) dupl_and_miss = [] (0..list1.length-1).each do |i| if list1.select { |w| w === list1[i] }.length > 1 dupl_and_miss.append list1[i] break end end (0..list1.length-1).each do |i| if not(list1.include? list1[0] + i) dupl_and_miss.append list1[0]+i break end end if dupl_and_miss.length > 1 return sprintf '(%s)', dupl_and_miss.join(',') else return -1 end end puts duplicate_and_missing([1,2,2,4]) puts duplicate_and_missing([1,2,3,4]) puts duplicate_and_missing([1,2,3,3])