유

기초 테스트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건
+
제목 글쓴이 날짜 조회
12-04-26 조회 3,865
12-04-24 조회 3,246
12-04-23 조회 4,574
12-04-22 조회 3,263
12-04-19 조회 3,356
12-04-18 조회 4,422
12-04-18 조회 3,120
12-04-17 조회 3,394
12-04-17 조회 5,149
12-04-16 조회 3,575
12-04-13 조회 3,123
12-04-09 조회 3,178
12-04-04 조회 3,954
12-04-01 조회 3,231
12-03-31 조회 3,633
12-03-30 조회 3,243
12-03-30 조회 3,907
12-03-26 조회 3,521
12-03-26 조회 3,090
12-03-22 조회 3,245
12-03-21 조회 3,101
12-03-19 조회 3,512
12-03-17 조회 3,530
12-03-14 조회 3,264
12-03-10 조회 3,937
12-03-09 조회 3,534
12-03-09 조회 3,355
12-03-08 조회 3,825
12-03-08 조회 3,991
12-03-08 조회 4,083
12-03-03 조회 3,349
12-03-02 조회 4,668
12-02-28 조회 3,193
12-02-27 조회 3,021
12-02-25 조회 3,161
12-02-24 조회 3,860
12-02-23 조회 3,129
12-02-22 조회 3,651
12-02-22 조회 5,413
12-02-22 조회 3,986
12-02-22 조회 4,306
12-02-18 조회 3,808
12-02-17 조회 3,028
12-02-17 조회 3,434
12-02-14 조회 3,504
12-02-13 조회 3,391
12-02-10 조회 1만
12-02-08 조회 3,051
12-02-07 조회 3,386
12-02-06 조회 3,114
12-02-05 조회 3,578
12-01-31 조회 3,504
12-01-30 조회 9,694
12-01-29 조회 3,848
12-01-26 조회 3,446
12-01-25 조회 4,058
12-01-24 조회 3,452
12-01-10 조회 3,755
12-01-10 조회 4,522
12-01-09 조회 3,292
12-01-08 조회 7,154
12-01-08 조회 3,677
12-01-07 조회 5,208
12-01-04 조회 3,909
12-01-02 조회 3,133
12-01-02 조회 3,183
12-01-02 조회 3,486
12-01-01 조회 3,121
11-12-31 조회 3,433
11-12-31 조회 3,116
11-12-30 조회 3,282
11-12-30 조회 3,438
11-12-29 조회 3,650
11-12-28 조회 3,739
11-12-26 조회 3,318
11-12-23 조회 3,538
11-12-22 조회 3,079
11-12-15 조회 4,172
11-12-08 조회 3,975
11-12-05 조회 3,317
11-12-04 조회 5,730
11-12-03 조회 3,609
11-12-02 조회 3,383
11-12-01 조회 5,457
11-11-30 조회 2,934
11-11-30 조회 2,836
11-11-28 조회 3,183
11-11-26 조회 3,377
11-11-25 조회 3,374
11-11-24 조회 2,974
11-11-22 조회 3,070
11-11-22 조회 2,910
11-11-17 조회 2,843
11-11-11 조회 2,839
11-11-09 조회 2,891
11-11-08 조회 3,272
11-11-08 조회 2,565
11-11-04 조회 3,651
11-11-01 조회 2,953
11-10-26 조회 2,542