Questions tagged [unique]

Refers to an element that is distinctly different from any other element in a collection.

Refers to an element that is distinctly different from any other element in a collection.

6072 questions
2731
votes
92 answers

Get all unique values in a JavaScript array (remove duplicates)

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on Stack Overflow that looks almost exactly like it,…
Mottie
  • 84,355
  • 30
  • 126
  • 241
2320
votes
54 answers

Remove duplicate values from JS array

I have a very simple JavaScript array that may or may not contain duplicates. var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"]; I need to remove the duplicates and put the unique values in a new array. I could point to all the code…
kramden88
  • 23,477
  • 3
  • 19
  • 17
1452
votes
23 answers

LINQ's Distinct() on a particular property

I am playing with LINQ to learn about it, but I can't figure out how to use Distinct when I do not have a simple list (a simple list of integers is pretty easy to do, this is not the question). What I if want to use Distinct on a List on…
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
1015
votes
31 answers

How do I remove duplicates from a list, while preserving order?

How do I remove duplicates from a list, while preserving order? Using a set to remove duplicates destroys the original order. Is there a built-in or a Pythonic idiom?
Josh Glover
  • 25,142
  • 27
  • 92
  • 129
827
votes
65 answers

How to get distinct values from an array of objects in JavaScript?

Assuming I have the following: var array = [ {"name":"Joe", "age":17}, {"name":"Bob", "age":17}, {"name":"Carl", "age": 35} ] What is the best way to be able to get an array of all of the distinct ages such that I…
Rolando
  • 58,640
  • 98
  • 266
  • 407
362
votes
4 answers

In Postgresql, force unique on combination of two columns

I would like to set up a table in PostgreSQL such that two columns together must be unique. There can be multiple values of either value, so long as there are not two that share both. For instance: CREATE TABLE someTable ( id int PRIMARY KEY…
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
360
votes
4 answers

Generate 'n' unique random numbers within a range

I know how to generate a random number within a range in Python. random.randint(numLow, numHigh) And I know I can put this in a loop to generate n amount of these numbers for x in range (0, n): listOfNumbers.append(random.randint(numLow,…
Chris Headleand
  • 6,003
  • 16
  • 51
  • 69
342
votes
4 answers

Count unique values per groups with Pandas

I need to count unique ID values in every domain. I have data: ID, domain 123, 'vk.com' 123, 'vk.com' 123, 'twitter.com' 456, 'vk.com' 456, 'facebook.com' 456, 'vk.com' 456, 'google.com' 789, 'twitter.com' 789, 'vk.com' I try df.groupby(['domain',…
Arseniy Krupenin
  • 3,800
  • 3
  • 13
  • 18
315
votes
8 answers

Select unique or distinct values from a list in UNIX shell script

I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this? For example, say my output is file suffixes in a…
brabster
  • 42,504
  • 27
  • 146
  • 186
276
votes
9 answers

Find the unique values in a column and then sort them

I have a pandas dataframe. I want to print the unique values of one of its columns in ascending order. This is how I am doing it: import pandas as pd df = pd.DataFrame({'A':[1,1,3,2,6,2,8]}) a = df['A'].unique() print a.sort() The problem is that I…
MAS
  • 4,503
  • 7
  • 32
  • 55
258
votes
10 answers

Postgres unique constraint vs index

As I can understand documentation the following definitions are equivalent: create table foo ( id serial primary key, code integer, label text, constraint foo_uq unique (code, label)); create table foo ( id serial primary key, …
Adam Piotrowski
  • 2,945
  • 3
  • 20
  • 23
257
votes
20 answers

Find unique rows in numpy.array

I need to find unique rows in a numpy.array. For example: >>> a # I have array([[1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 0]]) >>> new_a # I want to get to array([[1,…
Akavall
  • 82,592
  • 51
  • 207
  • 251
218
votes
16 answers

How do I count occurrence of unique values inside a list

So I'm trying to make this program that will ask the user for input and store the values in an array / list. Then when a blank line is entered it will tell the user how many of those values are unique. I'm building this for real life reasons and not…
Joel Aqu.
  • 2,289
  • 2
  • 13
  • 4
204
votes
12 answers

pandas unique values multiple columns

df = pd.DataFrame({'Col1': ['Bob', 'Joe', 'Bill', 'Mary', 'Joe'], 'Col2': ['Joe', 'Steve', 'Bob', 'Bob', 'Steve'], 'Col3': np.random.random(5)}) What is the best way to return the unique values of 'Col1' and…
user2333196
  • 5,406
  • 7
  • 31
  • 35
189
votes
14 answers

Count number of occurences for each unique value

Let's say I have: v = rep(c(1,2, 2, 2), 25) Now, I want to count the number of times each unique value appears. unique(v) returns what the unique values are, but not how many they are. > unique(v) [1] 1 2 I want something that gives me…
gakera
  • 3,589
  • 4
  • 30
  • 36
1
2 3
99 100