4

I am building a project that tells me the unique words in a piece of text.

I have my orginal string scriptTextView which I have added each word into the array scriptEachWordInArray

I would now like to create an array called scriptUniqueWords which only includes words that appear once (in other words are unique) in scriptEachWordInArray

So I'd like my scriptUniqueWords array to equal = ["Silent","Holy"] as a result.

I don't want to create an array without duplicates but an array that has only values that appeared once in the first place.

var scriptTextView = "Silent Night Holy Night"
var scriptEachWordInArray = ["Silent", "night", "Holy", "night"]
var scriptUniqueWords = [String]()

for i in 0..<scriptEachWordInArray.count {

    if scriptTextView.components(separatedBy: "\(scriptEachWordInArray[i]) ").count == 1 {
        scriptUniqueWords.append(scriptEachWordInArray[i])
        print("Unique word \(scriptEachWordInArray[i])")}

}
flakedev
  • 53
  • 1
  • 2
  • 6
  • 2
    Tried a `Set` ? – jtbandes Dec 21 '16 at 21:55
  • 2
    http://stackoverflow.com/a/29904817/456791, http://stackoverflow.com/questions/27624331/unique-values-of-array-in-swift – Bek Dec 21 '16 at 21:58
  • 1
    @jtbandes @Bek Thanks, but doesn't the `Set` technique remove all duplicates so each word will appear once instead of a possible multiple times. Night will appear in the array just once instead of twice. Whereas I am hoping to isolate just the unique values. I've tried this, or am I missing something. – flakedev Dec 21 '16 at 22:02
  • Reopening because I'm convinced this question is different from the others linked. However, I still think you could use Sets to solve the problem. – jtbandes Dec 21 '16 at 22:03
  • @jtbandes Sure, un-dupe-hammering is perfectly reasonable if you think I was wrong. – matt Dec 21 '16 at 22:29

4 Answers4

7

You can use NSCountedSet

let text = "Silent Night Holy Night"
let words = text.lowercased().components(separatedBy: " ")
let countedSet = NSCountedSet(array: words)
let singleOccurrencies = countedSet.filter { countedSet.count(for: $0) == 1 }.flatMap { $0 as? String }

Now singleOccurrencies contains ["holy", "silent"]

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
7

Swift

lets try It.

let array = ["1", "1", "2", "2", "3", "3"]
let unique = Array(Set(array))
// ["1", "2", "3"]
Community
  • 1
  • 1
Vishal Vaghasiya
  • 4,618
  • 3
  • 29
  • 40
2

Filtering out unique words without preserving order

As another alternative to NSCountedSet, you could use a dictionary to count the the number of occurrences of each word, and filter out those that only occur once:

let scriptEachWordInArray = ["Silent", "night", "Holy", "night"]

var freqs: [String: Int] = [:]
scriptEachWordInArray.forEach { freqs[$0] = (freqs[$0] ?? 0) + 1 }

let scriptUniqueWords = freqs.flatMap { $0.1 == 1 ? $0.0 : nil }
print(scriptUniqueWords) // ["Holy", "Silent"]

This solution, however (as well as the one using NSCountedSet), will not preserve the order of the original array, since a dictionary as well as NSCountedSet is an unordered collection.


Filtering out unique words while preserving order

If you'd like to preserve the order from the original array (removing element which appear more than once), you could count the frequencies of each word, but store it in a (String, Int) tuple array rather than a dictionary.

Making use of the Collection extension from this Q&A

extension Collection where Iterator.Element: Hashable {
    var frequencies: [(Iterator.Element, Int)] {
        var seen: [Iterator.Element: Int] = [:]
        var frequencies: [(Iterator.Element, Int)] = []
        forEach {
            if let idx = seen[$0] {
                frequencies[idx].1 += 1
            }
            else {
                seen[$0] = frequencies.count
                frequencies.append(($0, 1))
            }
        }
        return frequencies
    }
}

// or, briefer but worse at showing intent
extension Collection where Iterator.Element: Hashable {
    var frequencies: [(Iterator.Element, Int)] {
        var seen: [Iterator.Element: Int] = [:]
        var frequencies: [(Iterator.Element, Int)] = []
        for elem in self {
            seen[elem].map { frequencies[$0].1 += 1 } ?? {
                seen[elem] = frequencies.count
                return frequencies.append((elem, 1))
            }()
        }
        return frequencies
    }
}

... you may filter out the unique words of your array (while preserving order) as

let scriptUniqueWords = scriptEachWordInArray.frequencies
    .flatMap { $0.1 == 1 ? $0.0 : nil }

print(scriptUniqueWords) // ["Silent", "Holy"]
Community
  • 1
  • 1
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • Use NSCountedSet for this. – Alexander Dec 21 '16 at 22:16
  • 5
    @Alexander `NSCountedSet` is one good `Foundation` approach; diversity, imho, is interesting for SO answers (hence the "as another alternative" for the dictionary approach above). Also useful to include a method for when we'd like to preserve the order from the original array. – dfrib Dec 21 '16 at 22:45
  • 1
    @dfri you might be interested in this one https://stackoverflow.com/a/46376175/2303865 – Leo Dabus Feb 15 '19 at 04:08
-1

you can filter the values that are already contained in the array:

let newArray = array.filter { !array.contains($0) }
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
valentina
  • 11
  • 3
  • This would not work, because `array` already contains all its own elements. Hence the expression `!array.contains($0)` always return false. – Martin Nov 30 '17 at 05:11