Pergunta de entrevista da empresa Amazon

You are given an array with n positive integers where all values in the array are repeated except for one. Return the one that is not repeated.

Respostas da entrevista

Sigiloso

23 de jun. de 2012

public static int notRepeated(int[] given) { Map m = new HashMap(); for(i=0; i < given.length; i++) { if(m.get(given[i])) { m.put(given[i], 2); } else m.put(given[i], 1); for(x:m) { if(x == 1) return x; } } }

6

Sigiloso

17 de fev. de 2013

Cant use XOR as it fails when any element repeats odd number of times..... Can use hash map with O(n) time and O(n) space

1

Sigiloso

20 de fev. de 2013

only 2 possible solutions 1.) using sorting 2.) using additional space. which will be less than o(n)

1

Sigiloso

27 de out. de 2018

Use xor

1

Sigiloso

14 de out. de 2019

public static int NotRepeated (int ar[], int n) { int answer = -1; ArrayList arList = new ArrayList(); System.out.println(Arrays.toString(ar)); int n1 = 0; int n2 = 1; for (int i = 0; i < n; i++) { n1 = i; n2 = i +1; if (i == ar.length && (ar[i] != ar[i-1])) answer = ar[i]; else if (ar[n1] == ar[n2]) i++; else if (ar[0] != ar[1]) answer = ar[0]; else { if (ar[n1-1] == ar[n1]) { if (ar[n2] == ar[n2+1]) i++; else answer = ar[n2]; } else answer = ar[n1]; } }

Sigiloso

2 de dez. de 2015

sub find_odd { my @a = @{$_[0]}; my ($i, $n); $n = $a[0]; for $i (1 .. $#a) { $n = $n ^ $a[$i]; } printf("number is %s\n", $n); }

Sigiloso

16 de jul. de 2012

If you have an array of positive integers with only ONE non repeated integer, the easiest thing is just xor them all. Whatever you return will be the answer. Perl answer below sub findNotRepeated{ my ($arr) = @_; if(@$arr==0) { return - 1; } my $val = 0; foreach(@$arr) { $val ^= $_; } return($val); } sub findMissingElement{ my ($arr,$arr2) = @_; if(@$arr==0 || @$arr2 == 0 ) { print " arr2=" .@$arr2 . "X\n";; return - 1; } my $val = 0; foreach((@$arr , @$arr2)) { $val ^= $_; } return($val); }

4

Sigiloso

4 de jan. de 2013

This answer is in-place with O(n) complexity. A slight improvement over the space complexity of the Hash Map answer. public int returnNonRepeat(int[] input) { if(input.length < 3) return -1; else { int repeat = null; if(input[0] == input[1]) repeat = input[0]; else if(input[0] == input[2]) return input[1]; else return input[0]; for(int i = 2; i

Sigiloso

2 de ago. de 2012

first sort the array.n then 1)for i=1 to n { if ( element [i] !=element [i+1] ) { cout<

1