原文:
方法:
-
将所有输入整数放入哈希映射的键
-
在循环外部打印键集
java
import java.util.hashmap;
public class uniqueinarray2 {
public static void main(string args[])
{
int ar[] = { 10, 5, 3, 4, 3, 5, 6 };
hashmap hm = new hashmap();
for (int i = 0; i < ar.length; i ) {
hm.put(ar[i], i);
}
// using hm.keyset() to print output reduces time complexity. - lokesh
system.out.println(hm.keyset());
}
}
c
// c# implementation of the approach
using system;
using system.collections.generic;
public class uniqueinarray2
{
public static void main(string []args)
{
int []ar = { 10, 5, 3, 4, 3, 5, 6 };
dictionary hm = new dictionary();
for (int i = 0; i < ar.length; i )
{
if(hm.containskey(ar[i]))
hm.remove(ar[i]);
hm.add(ar[i], i);
}
// using hm.keys to print output
// reduces time complexity. - lokesh
var v = hm.keys;
foreach(int a in v)
console.write(a " ");
}
}
/* this code contributed by princiraj1992 */
给定一个整数数组,打印数组中所有不同的元素。 给定的数组可能包含重复项,并且输出应仅将每个元素打印一次。 给定的数组未排序。
示例:
input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}
output: 12, 10, 9, 45, 2
input: arr[] = {1, 2, 3, 4, 5}
output: 1, 2, 3, 4, 5
input: arr[] = {1, 1, 1, 1, 1}
output: 1
一个简单的pg电子试玩链接的解决方案是使用两个嵌套循环。 外循环从最左边的元素开始一个接一个地选择一个元素。 内部循环检查元素的左侧是否存在。 如果存在,则忽略该元素,否则打印该元素。 以下是简单算法的实现。
c
// c program to print all distinct elements in a given array
#include
using namespace std;
void printdistinct(int arr[], int n)
{
// pick all elements one by one
for (int i=0; i
java
// java program to print all distinct
// elements in a given array
import java.io.*;
class gfg {
static void printdistinct(int arr[], int n)
{
// pick all elements one by one
for (int i = 0; i < n; i )
{
// check if the picked element
// is already printed
int j;
for (j = 0; j < i; j )
if (arr[i] == arr[j])
break;
// if not printed earlier,
// then print it
if (i == j)
system.out.print( arr[i] " ");
}
}
// driver program
public static void main (string[] args)
{
int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
int n = arr.length;
printdistinct(arr, n);
}
}
// this code is contributed by vt_m
python3
# python program to print all distinct
# elements in a given array
def printdistinct(arr, n):
# pick all elements one by one
for i in range(0, n):
# check if the picked element
# is already printed
d = 0
for j in range(0, i):
if (arr[i] == arr[j]):
d = 1
break
# if not printed earlier,
# then print it
if (d == 0):
print(arr[i])
# driver program to test above function
arr = [6, 10, 5, 4, 9, 120, 4, 6, 10]
n = len(arr)
printdistinct(arr, n)
# this code is contributed by sam007.
c
// c# program to print all distinct
// elements in a given array
using system;
class gfg {
static void printdistinct(int []arr, int n)
{
// pick all elements one by one
for (int i = 0; i < n; i )
{
// check if the picked element
// is already printed
int j;
for (j = 0; j < i; j )
if (arr[i] == arr[j])
break;
// if not printed earlier,
// then print it
if (i == j)
console.write(arr[i] " ");
}
}
// driver program
public static void main ()
{
int []arr = {6, 10, 5, 4, 9, 120,
4, 6, 10};
int n = arr.length;
printdistinct(arr, n);
}
}
// this code is contributed by sam007.
php
输出:
6 10 5 4 9 120
上述pg电子试玩链接的解决方案的时间复杂度为o(n ^ 2)
。 我们可以使用排序来解决o(nlogn)
时间中的问题。 这个想法很简单,首先对数组进行排序,以便每个元素的所有出现变为连续。 一旦出现连续,我们就可以遍历排序后的数组并在o(n)
时间内打印出不同的元素。 以下是该想法的实现。
c
// c program to print all distinct elements in a given array
#include
using namespace std;
void printdistinct(int arr[], int n)
{
// first sort the array so that all occurrences become consecutive
sort(arr, arr n);
// traverse the sorted array
for (int i=0; i
java
// java program to print all distinct
// elements in a given array
import java.io.*;
import java .util.*;
class gfg
{
static void printdistinct(int arr[], int n)
{
// first sort the array so that
// all occurrences become consecutive
arrays.sort(arr);
// traverse the sorted array
for (int i = 0; i < n; i )
{
// move the index ahead while
// there are duplicates
while (i < n - 1 && arr[i] == arr[i 1])
i ;
// print last occurrence of
// the current element
system.out.print(arr[i] " ");
}
}
// driver program
public static void main (string[] args)
{
int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
int n = arr.length;
printdistinct(arr, n);
}
}
// this code is contributed by vt_m
python3
# python program to print all distinct
# elements in a given array
def printdistinct(arr, n):
# first sort the array so that
# all occurrences become consecutive
arr.sort();
# traverse the sorted array
for i in range(n):
# move the index ahead while there are duplicates
if(i < n-1 and arr[i] == arr[i 1]):
while (i < n-1 and (arr[i] == arr[i 1])):
i =1;
# prlast occurrence of the current element
else:
print(arr[i], end=" ");
# driver code
arr = [6, 10, 5, 4, 9, 120, 4, 6, 10];
n = len(arr);
printdistinct(arr, n);
# this code has been contributed by 29ajaykumar
c
// c# program to print all distinct
// elements in a given array
using system;
class gfg {
static void printdistinct(int []arr, int n)
{
// first sort the array so that
// all occurrences become consecutive
array.sort(arr);
// traverse the sorted array
for (int i = 0; i < n; i )
{
// move the index ahead while
// there are duplicates
while (i < n - 1 && arr[i] == arr[i 1])
i ;
// print last occurrence of
// the current element
console.write(arr[i] " ");
}
}
// driver program
public static void main ()
{
int []arr = {6, 10, 5, 4, 9, 120, 4, 6, 10};
int n = arr.length;
printdistinct(arr, n);
}
}
// this code is contributed by sam007.
php
输出:
4 5 6 9 10 120
我们可以使用散列来平均解决o(n)
时间。 这个想法是从左到右遍历给定的数组,并在哈希表中跟踪访问的元素。 以下是该想法的实现。
c
/* cpp program to print all distinct elements
of a given array */
#include
using namespace std;
// this function prints all distinct elements
void printdistinct(int arr[],int n)
{
// creates an empty hashset
unordered_set s;
// traverse the input array
for (int i=0; i
java
/* java program to print all distinct elements of a given array */
import java.util.*;
class main
{
// this function prints all distinct elements
static void printdistinct(int arr[])
{
// creates an empty hashset
hashset set = new hashset<>();
// traverse the input array
for (int i=0; i
python3
# python3 program to print all distinct elements
# of a given array
# this function prints all distinct elements
def printdistinct(arr, n):
# creates an empty hashset
s = dict();
# traverse the input array
for i in range(n):
# if not present, then put it in
# hashtable and print it
if (arr[i] not in s.keys()):
s[arr[i]] = arr[i];
print(arr[i], end = " ");
# driver code
arr = [10, 5, 3, 4, 3, 5, 6];
n = 7;
printdistinct(arr, n);
# this code is contributed by princi singh
c
// c# program to print all distinct
// elements of a given array
using system;
using system.collections.generic;
class gfg
{
// this function prints all
// distinct elements
public static void printdistinct(int[] arr)
{
// creates an empty hashset
hashset set = new hashset();
// traverse the input array
for (int i = 0; i < arr.length; i )
{
// if not present, then put it
// in hashtable and print it
if (!set.contains(arr[i]))
{
set.add(arr[i]);
console.write(arr[i] " ");
}
}
}
// driver code
public static void main(string[] args)
{
int[] arr = new int[] {10, 5, 3, 4, 3, 5, 6};
printdistinct(arr);
}
}
// this code is contributed by shrikant13
输出:
10 5 3 4 6
哈希优于排序的另一个优点是,元素的打印顺序与输入数组中的顺序相同。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处