Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
First Interview question - Java
/** Write an algorithm that will find the first duplicate in a list on the page - {6, 3, 1, 4, 7, 4, 2, 8, 9, 2} For example we would return 4, for the list above. */ import java.util.*; import java.lang.*; class Rextester { static int[] myList = {6, 3, 1, 4, 7, 4, 2, 8, 9, 2}; public static void main(String args[]) { int result = findDuplicate(); System.out.println((result > 0)? "Found the first duplicate: " + result : "First duplicate not found"); } static int findDuplicate() { Hashtable<Integer, Integer> myHash = new Hashtable<Integer, Integer>(); for (int i = 0; i < myList.length; i++) { if (!myHash.contains(new Integer(myList[i]))) { // add this value to myHash myHash.put(i, myList[i]); } else if (myHash.containsValue(new Integer(myList[i]))) { // found the duplicate return myList[i]; } } return -1; } }
run
|
edit
|
history
|
help
0
LinearSearch
Javaprog
Umang Khambhalikar
Odd Numbers
word Ladder
Aaina
LeetCode 121 - Best time to buy and sell stock - O(n) solution
Find Median in Large File of Integers
LCM of 2 numbers
Coloring map