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

(새 문서: ==PHP== 분류: PHP {{참고|PHP array_unique()}} <source lang='php'> $arr = [ 1,4,5,4,4,3,2,1 ]; $arr2 = array_unique( $arr ); print_r( $arr2 ); # Array # ( # [0] => 1 #...)
 
 
(사용자 2명의 중간 판 20개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: unique]]
{{DISPLAYTITLE: 함수 array_unique()}}
==개요==
;함수 array_unique()
* 중복되는 원소를 제거하고 unique한 원소만 남기는 함수
==C++==
[[분류: C++]]
{{참고|C++ unique()}}
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    vector<int> v = {1,4,5,4,4,3,2,1};
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    for(const int& x: v) cout << x << ' '; // 1 2 3 4 5
}
</syntaxhighlight>
==Java==
[[분류: Java]]
{{참고|Java array_unique()}}
<syntaxhighlight lang='java' run>
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]
    }
}
</syntaxhighlight>
==JavaScript==
[[분류: JavaScript]]
{{참고|자바스크립트 array_unique()}}
<syntaxhighlight lang='javascript' run>
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 ]
</syntaxhighlight>
==PHP==
==PHP==
[[분류: PHP]]
[[분류: PHP]]
{{참고|PHP array_unique()}}
{{참고|PHP array_unique()}}
<source lang='php'>
<syntaxhighlight lang='php' run>
$arr = [ 1,4,5,4,4,3,2,1 ];
$arr = [ 1,4,5,4,4,3,2,1 ];
$arr2 = array_unique( $arr );
$arr2 = array_unique( $arr );
print_r( $arr2 );
print_r( $arr2 );
# Array
</syntaxhighlight>
# (
<syntaxhighlight lang='php' run>
#    [0] => 1
#    [1] => 4
#    [2] => 5
#    [5] => 3
#    [6] => 2
# )
</source>
<source lang='php'>
$arr = [ 'a'=>'hello', 'b'=>'world', 'c'=>'hello' ];
$arr = [ 'a'=>'hello', 'b'=>'world', 'c'=>'hello' ];
$arr2 = array_unique( $arr );
$arr2 = array_unique( $arr );
print_r( $arr2 );
print_r( $arr2 );
# Array
</syntaxhighlight>
# (
 
#     [a] => hello
==Python==
#    [b] => world
[[분류: Python]]
# )
{{참고|Python array_unique()}}
</source>
<syntaxhighlight lang='python' run>
a = [ 1,4,5,4,4,3,2,1 ]
b = list(set(a))
print( b ) # [1, 2, 3, 4, 5]
</syntaxhighlight>
 
==R==
[[분류: R]]
{{참고|R unique()}}
<syntaxhighlight lang='r' run>
a <- c(1,4,5,4,4,3,2,1)
b <- unique(a)
b
</syntaxhighlight>
 
==같이 보기==
* [[함수 multi_array_unique()]]
* [[함수 unique_array_by_value()]]

2024년 1월 5일 (금) 02:03 기준 최신판


1 개요[ | ]

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

2 C++[ | ]

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> v = {1,4,5,4,4,3,2,1};
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    for(const int& x: v) cout << x << ' '; // 1 2 3 4 5 
}

3 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]
    }
}

4 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 ]

5 PHP[ | ]

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

6 Python[ | ]

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

7 R[ | ]

a <- c(1,4,5,4,4,3,2,1) 
b <- unique(a)
b

8 같이 보기[ | ]

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