Apple interview question

Use objective-c write code to return the first repeated integer from a given array, with O(n) time

Interview Answers

Anonymous

25 Aug 2014

Use a Hash Table to insert each element (in our case number) - search and insert of each of the N element is O(1) Search and Insert all N elements is O(N) Whenever a given elements already exist at the Hash Table output this element

6

Anonymous

7 Sept 2014

private static void findDuplicate() { int arrayInt[] = { 2, 4, 56, 2, 4, 7, 8 }; HashMap map = new HashMap(); try { for (int i = 0; i < arrayInt.length; i++) { if (!map.containsKey(arrayInt[i])) { map.put(arrayInt[i], arrayInt[i]); } else { System.out.println("duplicate item:" + arrayInt[i] + " at index: " + i); } } } catch (Exception e) { e.printStackTrace(); } }

2

Anonymous

2 Oct 2018

A NSSet is enough.

Anonymous

4 Jun 2019

if the array is sorted, you could XOR consecutive integers and return element whose XOR result is 0

Anonymous

15 Aug 2015

Is there any solution better than hashtable?