유

기초 테스트1

array_unique

주어진 인자로 주어진 배열의 값들 중, 중복되는 값을 제외하고, 중복이 없는 것만을 모아 새로운 배열로 만든후 반환합니다.
 
Takes an input array and returns a new array without duplicate values.
 
array_unique 를 사용 하면 아래와 같이 쉽게 중복을 제거한 배열을 얻을수 있습니다.
 
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
Array
(
    [a] => green
    [0] => red
    [1] => blue
)
 
문제> array_unique 를 사용하지 않고 위와 동일한 문제에 동일한 답이 나오도록 처리하세요.
제한시간 - 30분
|

댓글 16개

우와 신선한데요?

function array_unique_custom($arr){

while (list($key, $value) = each($arr)) {
if($$value == true) unset($arr[$key]);
$$value = true;
}
return $arr;

}
in_array면 간단할거 같은데 그 함수도 안되면 루프를 2번 돌려야될듯;;
상관없습니다. 문제 그대로 입니다.
코딩하기는 싫고,,
key -> value 구조를 value->key 구조로 convert 하신후 다시 value->key를 한번더 돌리면 됩니다.

$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$temp = array();
$result = array();
foreach($input as $key=>$value) {
if(!isset($temp["{$value}"]))
$temp["{$value}"] = $key;

}

foreach($temp as $key=>$value) {
$result["{$value}"] = $key;

}
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = xarray_unique($input);
print_r($result);



function xarray_unique($input) {
$rtn=array();
while (list($key,$value)=each($input)) {
if (!in_array($value,$rtn,true)) {
$rtn["{$key}"]=$value;
}
}
return $rtn;
}
저도 명랑폐인님 같은 생각했는데, 그냥

$input = array("a" => "green", "red", "b" => "green", "blue", "red");

print_r(array_merge(array_flip(array_flip($input))));
오 이건 생각의 전환인데요...멋집니다
저도

<?
$input = array("a" => "green", "red", "b" => "green", "blue", "red");

$output = array();
foreach($input as $k=>$v) {
if (!in_array($v, $output)) {
$output[$k] = $v;
}
}
print_r($output);
?>

Array
(
[a] => green
[0] => red
[1] => blue
)
print_r(array_flip(array_flip(array_reverse($input, true))));
WoW !
이러니 배추빌더가 쵝오지!!!
털석OTL
연구해보니 답은 맞습니다만. 이거 태클의 여지가 좀...
어째든 대단하십니다. 발상자체가 다르군요.
마치 무림고수처럼... 휘리릭!!!
우와 단 한줄로 끝내시네요~ 저도 배추빌더 회원인데 ㅋㅋㅋ
헉, 과찬들이십니다.
다른 분들 답변보고 다른 방법은 없을까? 하며
매뉴얼 뒤져본 것 뿐입니다.
저는 인터넷 안되면 코딩을 못한다는....>.<
기초는 아닌듯 ^^
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
11-10-25 조회 2,492
11-10-18 조회 4,024
11-10-18 조회 2,397
11-10-15 조회 3,617
11-10-10 조회 2,672
11-10-02 조회 2,590
11-09-28 조회 2,403
11-09-24 조회 2,488
11-09-22 조회 2,389
11-09-05 조회 2,875
11-08-31 조회 2,251
11-08-31 조회 2,559
11-08-31 조회 2,413
11-08-29 조회 2,962
11-08-28 조회 2,458
11-08-25 조회 2,509
11-08-17 조회 2,226
11-08-16 조회 2,461
11-08-11 조회 2,915
11-08-07 조회 2,627
11-08-03 조회 2,339
11-07-22 조회 2,083
11-07-12 조회 3,301
11-06-27 조회 2,084
11-06-01 조회 1,750
11-05-17 조회 2,536
11-05-01 조회 2,607
11-04-28 조회 2,892
11-04-28 조회 2,944
11-04-13 조회 2,602
11-03-27 조회 2,726
11-03-27 조회 2,603
11-03-14 조회 2,015
11-03-02 조회 2,339
11-03-02 조회 2,568
11-02-27 조회 2,999
11-02-23 조회 2,378
11-02-19 조회 2,519
11-02-14 조회 4,133
11-02-03 조회 2,928
11-01-24 조회 3,870
11-01-04 조회 2,401
10-12-08 조회 5,412
10-11-15 조회 2,420
10-11-14 조회 2,593
10-11-14 조회 3,613
10-10-29 조회 3,685
10-10-19 조회 2,697
10-10-07 조회 2,525
10-10-05 조회 4,446
10-09-18 조회 2,744
10-09-18 조회 2,481
10-09-17 조회 4,886
10-09-15 조회 2,824
10-09-15 조회 2,352
10-09-15 조회 2,843
10-09-15 조회 2,673
10-09-11 조회 3,999
10-09-11 조회 2,506
10-09-11 조회 2,544
10-09-07 조회 3,819
10-09-07 조회 3,354
10-08-29 조회 2,814
10-07-01 조회 3,063
10-06-30 조회 2,468
10-06-30 조회 2,651
10-06-30 조회 3,003
10-06-13 조회 2,038
10-04-15 조회 3,209
10-04-04 조회 3,439
10-02-23 조회 3,536
10-02-09 조회 3,139
10-01-25 조회 2,649
10-01-25 조회 2,745
09-12-27 조회 4,394
09-11-23 조회 3,616
09-11-20 조회 4,850
09-08-30 조회 3,048
09-08-30 조회 3,044
09-08-12 조회 3,014
09-07-22 조회 4,074
09-07-22 조회 4,720
09-07-15 조회 5,889
09-07-11 조회 2,965
09-06-17 조회 2,708
09-06-15 조회 2,993
09-05-29 조회 3,272
09-05-11 조회 5,911
09-05-08 조회 2,666
09-04-30 조회 3,783
09-04-17 조회 3,101
09-04-04 조회 3,282
09-03-05 조회 3,667
09-02-09 조회 3,637
09-02-03 조회 6,140
08-12-27 조회 3,206
08-12-20 조회 4,296
08-11-21 조회 5,256
08-11-14 조회 2,824
08-11-03 조회 3,285