"함수 array unique()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>))
잔글 (봇: Jmnote의 2020-10-26T06:24:00Z 에 작성한 602708 판으로 되돌림)
46번째 줄: 46번째 줄:
#    [6] => 2
#    [6] => 2
# )
# )
</syntaxhighlight>
</source>
<source lang='php'>
<source lang='php'>
$arr = [ 'a'=>'hello', 'b'=>'world', 'c'=>'hello' ];
$arr = [ 'a'=>'hello', 'b'=>'world', 'c'=>'hello' ];
56번째 줄: 56번째 줄:
#    [b] => world
#    [b] => world
# )
# )
</syntaxhighlight>
</source>


==Python==
==Python==
66번째 줄: 66번째 줄:
print( b )
print( b )
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
</syntaxhighlight>
</source>


==R==
==R==
76번째 줄: 76번째 줄:
cat( b )
cat( b )
## 1 4 5 3 2
## 1 4 5 3 2
</syntaxhighlight>
</source>


==같이 보기==
==같이 보기==
* [[함수 multi_array_unique()]]
* [[함수 multi_array_unique()]]
* [[함수 unique_array_by_value()]]
* [[함수 unique_array_by_value()]]

2020년 11월 2일 (월) 00:50 판


1 개요

함수 array_unique()
  • 중복되는 원소를 제거하고 unique한 원소만 남기는 함수

2 Java

import java.util.Arrays;
import java.util.stream.IntStream;
public class MyClass {
    public static void main(String args[]) {
        int[] arr = {1,4,5,4,4,3,2,1};
        int[] arr2 = IntStream.of(arr).distinct().toArray();
        System.out.println( Arrays.toString(arr2) );
        // [1, 4, 5, 3, 2]
    }
}

3 JavaScript

let arr = [1,4,5,4,4,3,2,1];
let arr2 = arr.filter((value, index, self) => { return self.indexOf(value) === index; });
console.log(arr2); 
// [ 1, 4, 5, 3, 2 ]

4 PHP

$arr = [ 1,4,5,4,4,3,2,1 ];
$arr2 = array_unique( $arr );
print_r( $arr2 );
# Array
# (
#     [0] => 1
#     [1] => 4
#     [2] => 5
#     [5] => 3
#     [6] => 2
# )
$arr = [ 'a'=>'hello', 'b'=>'world', 'c'=>'hello' ];
$arr2 = array_unique( $arr );
print_r( $arr2 );
# Array
# (
#     [a] => hello
#     [b] => world
# )

5 Python

a = [ 1,4,5,4,4,3,2,1 ]
b = list(set(a))
print( b )
# [1, 2, 3, 4, 5]

6 R

a = c(1,4,5,4,4,3,2,1) 
b = unique(a)
cat( b )
## 1 4 5 3 2

7 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}