Question d’entretien chez Apple

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

Réponses aux questions d'entretien

Utilisateur anonyme

25 août 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

Utilisateur anonyme

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

Utilisateur anonyme

2 oct. 2018

A NSSet is enough.

Utilisateur anonyme

4 juin 2019

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

Utilisateur anonyme

15 août 2015

Is there any solution better than hashtable?