기초 테스트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
연구해보니 답은 맞습니다만. 이거 태클의 여지가 좀...
어째든 대단하십니다. 발상자체가 다르군요.
마치 무림고수처럼... 휘리릭!!!
우와 단 한줄로 끝내시네요~ 저도 배추빌더 회원인데 ㅋㅋㅋ
헉, 과찬들이십니다.
다른 분들 답변보고 다른 방법은 없을까? 하며
매뉴얼 뒤져본 것 뿐입니다.
저는 인터넷 안되면 코딩을 못한다는....>.<
기초는 아닌듯 ^^
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
14년 전 조회 2,660
14년 전 조회 755
14년 전 조회 2,590
14년 전 조회 1,705
14년 전 조회 933
14년 전 조회 1,150
14년 전 조회 1,048
14년 전 조회 1,031
14년 전 조회 562
14년 전 조회 763
14년 전 조회 864
14년 전 조회 1,234
14년 전 조회 928
14년 전 조회 1,286
14년 전 조회 1,061
14년 전 조회 1,165
14년 전 조회 767
14년 전 조회 1,076
14년 전 조회 980
14년 전 조회 851
14년 전 조회 738
14년 전 조회 1,223
14년 전 조회 1,627
14년 전 조회 989
14년 전 조회 834
14년 전 조회 3,358
14년 전 조회 887
14년 전 조회 762
14년 전 조회 2,151
14년 전 조회 1,561
14년 전 조회 2,078
14년 전 조회 640
14년 전 조회 894
14년 전 조회 746
14년 전 조회 584
14년 전 조회 669
14년 전 조회 797
14년 전 조회 1,541
14년 전 조회 898
14년 전 조회 963
14년 전 조회 576
14년 전 조회 1,106
14년 전 조회 1,698
14년 전 조회 1,083
14년 전 조회 1,371
14년 전 조회 885
14년 전 조회 1,026
14년 전 조회 816
14년 전 조회 1,149
14년 전 조회 1,039
14년 전 조회 464
14년 전 조회 599
14년 전 조회 664
14년 전 조회 986
14년 전 조회 806
14년 전 조회 811
14년 전 조회 790
14년 전 조회 1,195
14년 전 조회 901
14년 전 조회 907
14년 전 조회 913
14년 전 조회 722
14년 전 조회 1,780
14년 전 조회 1,548
14년 전 조회 980
14년 전 조회 807
14년 전 조회 1,875
14년 전 조회 1,244
14년 전 조회 1,080
14년 전 조회 837
14년 전 조회 1,544
14년 전 조회 3,803
14년 전 조회 2,464
14년 전 조회 1,158
14년 전 조회 771
14년 전 조회 828
14년 전 조회 1,354
14년 전 조회 1,014
14년 전 조회 1,389
14년 전 조회 1,436
14년 전 조회 1,457
14년 전 조회 657
14년 전 조회 899
14년 전 조회 2,776
14년 전 조회 801
14년 전 조회 1,276
14년 전 조회 846
14년 전 조회 719
14년 전 조회 1,264
14년 전 조회 699
14년 전 조회 1,370
14년 전 조회 956
14년 전 조회 1,619
14년 전 조회 1,542
14년 전 조회 1,059
14년 전 조회 1,290
14년 전 조회 971
14년 전 조회 1,681
14년 전 조회 1,256
14년 전 조회 1,253