Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution2 does not return the correct int if list order is changed #6

Open
alexandrefrigout opened this issue May 3, 2019 · 0 comments

Comments

@alexandrefrigout
Copy link

If the main has the following code System.out.println(solution(new int[] { 7, 9, 3, 9, 3, 9, 9 })); an incorrect number is returned by solution2.
The below code for solution2 works well and is of same order:

public static int solution2(int[] A) {
	// write your code in Java SE 8
	HashSet<Integer> dups = new HashSet<Integer>();
	ArrayList<Integer> tmp = new ArrayList<>();
	for (int i = 0; i < A.length; i++) {
		if (dups.add(A[i])) {
			tmp.add(A[i]);
		}
		else{
			tmp.remove(new Integer(A[i]));
		}
	}
	return tmp.get(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant