Question: Write a method int firstDuplicate(int[] a) that returns the first duplicate in array a. If there are no duplicates, return -1. You may assume that
Write a method int firstDuplicate(int[] a) that returns the first duplicate in array a. If there are no duplicates, return -1. You may assume that the value of every element is in the range of 0 to n-1 where n is the array length. The runtime must be O(n). Use a boolean array of size n to keep track of which elements you've seen so far.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
