Pergunta de entrevista da empresa Meta

Given an input array, remove all any duplicate occurrences and return the array.

Respostas da entrevista

Sigiloso

10 de dez. de 2015

private static int[] removeDuplicates(int[] a) { int[] b = new int[a.length]; Map map = new HashMap(); int index = 0; for(int i = 0; i < a.length; i++) { if(!map.containsKey(a[i])) { map.put(a[i], 1); b[index] = a[i]; index++; } } return b; }

2

Sigiloso

26 de out. de 2016

above answer isn't optimal. no need for map, we can use set. possibly no need to create new array, we can use the current one.

Sigiloso

24 de out. de 2017

You can do list(set(arr)) in python

Sigiloso

17 de jan. de 2019

arr=>Array.from(new Set([...arr]))

Sigiloso

7 de fev. de 2020

function repeatData($arr){ $a=0; for($i=0;$i<=count($arr);$i++){ if($a==$arr[$i]){ return $a;exit; }else if(in_array($arr[$i],$data)){ return $arr[$i]; }else{ $a = $arr[$i]; $data[]= $a; } } } //$array_val = array(1,2,1,1,3); //$array_val = array(1,2,2,3); $val = repeatData($array_val); echo ' '.$val;