原文:
给定一个可能包含重复项的数组,请按排序顺序打印所有不同的元素。
例子:
input : 1, 3, 2, 2, 1
output : 1 2 3
input : 1, 1, 1, 2, 2, 3
output : 1 2 3
简单pg电子试玩链接的解决方案首先对数组进行排序,然后遍历数组并仅打印元素的第一个匹配项。
另一种方法是使用 c stl 中的 。
c
// cpp program to print sorted distinct
// elements.
#include
using namespace std;
void printrepeating(int arr[], int size)
{
// create a set using array elements
set s(arr, arr size);
// print contents of the set.
for (auto x : s)
cout << x << " ";
}
// driver code
int main()
{
int arr[] = { 1, 3, 2, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
printrepeating(arr, n);
return 0;
}
java
// java program to print sorted distinct
// elements.
import java.io.*;
import java.util.*;
public class gfg {
static void printrepeating(integer []arr, int size)
{
// create a set using array elements
sortedset s = new treeset<>();
collections.addall(s, arr);
// print contents of the set.
system.out.print(s);
}
// driver code
public static void main(string args[])
{
integer []arr = {1, 3, 2, 2, 1};
int n = arr.length;
printrepeating(arr, n);
}
}
// this code is contributed by
// manish shaw (manishshaw1)
python3
# python3 program to print
# sorted distinct elements.
def printrepeating(arr,size):
# create a set using array elements
s = set()
for i in range(size):
if arr[i] not in s:
s.add(arr[i])
# print contents of the set.
for i in s:
print(i,end=" ")
# driver code
if __name__=='__main__':
arr = [1,3,2,2,1]
size = len(arr)
printrepeating(arr,size)
# this code is contributed by
# shrikant13
c
// c# program to print sorted distinct
// elements.
using system;
using system.collections.generic;
using system.linq;
class gfg {
static void printrepeating(int []arr, int size)
{
// create a set using array elements
sortedset s = new sortedset(arr);
// print contents of the set.
foreach (var n in s)
{
console.write(n " ");
}
}
// driver code
public static void main()
{
int []arr = {1, 3, 2, 2, 1};
int n = arr.length;
printrepeating(arr, n);
}
}
// this code is contributed by
// manish shaw (manishshaw1)
输出:
1 2 3
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处