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
extends
OBF-7 4
2 dimensional array (filled in by the user)
exp 4
rd dap da
VENU DEVELOPMENTS
BinaryTreeFindNext_noParentPointer
test1
Kadane's Algorithm
상속1