함수 array_unique()


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